How to make MongoDB find nested json return as an array?

augustoccesar

I have this data:

{
    "_id" : ObjectId("5461e16ee7caf96f8f3584a2"),
    "num_marcacao" : "100",
    "sexo" : "Fêmea",
    "idade" : "20",
    "bigdata" : {
        "abortos" : [ 
            {
                "data_aborto" : "2014-11-11",
                "causa_aborto" : "Aborto causa 1"
            }, 
            {
                "data_aborto" : "2014-09-01",
                "causa_aborto" : "Aborto causa 2"
            }
        ],
        "crias" : [ 
            ObjectId("5461e16ee7caf96f8f3584a2")
         ]
     }
}
{
    "_id" : ObjectId("5461e1cae7caf96f8f3584a4"),
    "num_marcacao" : "200",
    "sexo" : "Fêmea",
    "bigdata" : {
       "crias" : [ 
           ObjectId("5461e1f3e7caf96f8f3584a5"), 
           ObjectId("5461e760e7caf96f8f3584a6")
       ]
    }
}

Using the following distinct function I get one result

db.animal.distinct('_id', {'bigdata.crias':{$exists:true}}

Result:

{
    "0" : ObjectId("5461e16ee7caf96f8f3584a2"),
    "1" : ObjectId("5461e1cae7caf96f8f3584a4")
}    

Now I want to get the array that is in bigdata.crias like the result of the distinct query. I'm trying to do like this:

db.animal.find(
    {
        $and: [
            {'num_marcacao': '200'},
            {'bigdata.crias':{$exists: true}}
        ]
    },
    {
        'bigdata.crias': true,
        '_id': false
    }
)

But the result is not like the one I need. This is what it's returning:

{
    "bigdata" : {
        "crias" : [ 
            ObjectId("5461e1f3e7caf96f8f3584a5"), 
            ObjectId("5461e760e7caf96f8f3584a6")
        ]
    }
}

And I need

{
    "0" : ObjectId("5461e1f3e7caf96f8f3584a5"),
    "1" : ObjectId("5461e760e7caf96f8f3584a6")
} 
Neil Lunn

Anyhow. MongoDB does not generally do this from either the .find() or .aggregate() methods or anything general around them. Only the .distinct() method invokes a special form where the result given is "truly" just an array of the specified "key" to be distinct on.

You can always "inspect" the object returned and just use the array element in the structure. You can also specify a "query" argument to the .distinct() command method in the first place:

db.collection.distinct(
    "bigdata.crias",
    { 
        "bigdata.crias": { "$exists": true },
        "num_marcacao": "200"
    }
);

Where you also see your $and argument is redundant. All MongoDB query arguments are an "and" implementation by default. You don't need this unless you are specifying "more than one" condition on the same "field name". That would result in an invalid object by breaking the basic "hash/map" "unique key" rule, and which is why and "array" is used for this form to keep it valid.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MongoDB: Make a projection to return just the first element of an array nested to another array nested to an object

From Dev

How to find index of Json nested array in jquery?

From Dev

Find in Double Nested Array MongoDB

From Dev

How to retrieve / find all elements of a nested array in MongoDB Java

From Dev

how can I access a nested array within a json data return?

From Dev

How to make sub rows for json nested array in jqgrid

From Dev

mongoDB query to find the document in nested array

From Dev

MongoDB find key on nested object KEY (JSON)

From Dev

MongoDB: How to find specific value in nested array, if found then show only that array not all?

From Dev

Serializing MongoDB find() return into non-anonymous JSON array, using PyMongo

From Dev

Mongodb: Query a json-object nested in an array

From Dev

retrieve values from nested json array in mongodb

From Dev

Mongodb: Query a json-object nested in an array

From Dev

How to find an element in an array in mongodb?

From Dev

How to create a nested array JSON?

From Dev

How to parse nested JSON array?

From Dev

MongoDB: Return objects in a nested array that has the most recent date

From Dev

mongodb node - find documents by array of json objects

From Dev

How to return a filtered nested array in RethinkDB?

From Java

How to join with nested array in MongoDB driver for NodeJS?

From Dev

How to update names field nested in an array in mongodb

From Dev

how to query nested array of objects in mongodb?

From Dev

How to update data in nested array in MongoDB

From Dev

MongoDB - How to $inc in a double nested array

From Dev

how do I find out a JSON Object return JSON Array or string in android

From Dev

how do I find out a JSON Object return JSON Array or string in android

From Dev

How to find all in an array of array in MongoDB

From Dev

How to implement API to return a nested JSON?

From Dev

Underscore.js Find and return element in nested array

Related Related

  1. 1

    MongoDB: Make a projection to return just the first element of an array nested to another array nested to an object

  2. 2

    How to find index of Json nested array in jquery?

  3. 3

    Find in Double Nested Array MongoDB

  4. 4

    How to retrieve / find all elements of a nested array in MongoDB Java

  5. 5

    how can I access a nested array within a json data return?

  6. 6

    How to make sub rows for json nested array in jqgrid

  7. 7

    mongoDB query to find the document in nested array

  8. 8

    MongoDB find key on nested object KEY (JSON)

  9. 9

    MongoDB: How to find specific value in nested array, if found then show only that array not all?

  10. 10

    Serializing MongoDB find() return into non-anonymous JSON array, using PyMongo

  11. 11

    Mongodb: Query a json-object nested in an array

  12. 12

    retrieve values from nested json array in mongodb

  13. 13

    Mongodb: Query a json-object nested in an array

  14. 14

    How to find an element in an array in mongodb?

  15. 15

    How to create a nested array JSON?

  16. 16

    How to parse nested JSON array?

  17. 17

    MongoDB: Return objects in a nested array that has the most recent date

  18. 18

    mongodb node - find documents by array of json objects

  19. 19

    How to return a filtered nested array in RethinkDB?

  20. 20

    How to join with nested array in MongoDB driver for NodeJS?

  21. 21

    How to update names field nested in an array in mongodb

  22. 22

    how to query nested array of objects in mongodb?

  23. 23

    How to update data in nested array in MongoDB

  24. 24

    MongoDB - How to $inc in a double nested array

  25. 25

    how do I find out a JSON Object return JSON Array or string in android

  26. 26

    how do I find out a JSON Object return JSON Array or string in android

  27. 27

    How to find all in an array of array in MongoDB

  28. 28

    How to implement API to return a nested JSON?

  29. 29

    Underscore.js Find and return element in nested array

HotTag

Archive