2

i have this collection in mongodb :

db.persons.find().pretty();{
"_id" : ObjectId("564635a944ae32e021f8b81b"),
"items" : [
        {
                "_id" : ObjectId("564635a944ae32e021f8b819"),
                "personId" : 1,
                "first_name" : "A",
                "last_name" : "B",
                "gender" : "m",
                "comment" : "123"
        },
        {
                "_id" : ObjectId("564635a944ae32e021f8b81a"),
                "personId" : 2,
                "first_name" : "w",
                "last_name" : "X",
                "gender" : "m",
                "comment" : "987"
        }
]},{
"_id" : ObjectId("564635a944ae32e021f8b812"),
"items" : [
        {
                "_id" : ObjectId("564635a944ae32e021f8b8d5"),
                "personId" : 3,
                "first_name" : "Y",
                "last_name" : "Z",
                "gender" : "f",
                "comment" : "357"
        }]}

with this command(findOne) :

> db.persons.findOne({"items.personId" : 1})

get this result :

{
"_id" : ObjectId("564635a944ae32e021f8b81b"),
"items" : [
        {
                "_id" : ObjectId("564635a944ae32e021f8b819"),
                "personId" : 1,
                "first_name" : "A",
                "last_name" : "B",
                "gender" : "m",
                "comment" : "123"
        },
        {
                "_id" : ObjectId("564635a944ae32e021f8b81a"),
                "personId" : 2,
                "first_name" : "w",
                "last_name" : "X",
                "gender" : "m",
                "comment" : "987"
        }
]}

but; how I get this result : only first_name & last_name & _id from items that personId = 1

        {                    
                "_id" : ObjectId("564635a944ae32e021f8b819"),
                "first_name" : "A",
                "last_name" : "B"
        }

(just with the findOne command(No aggregate)). very thanks...

2 Answers 2

1

Best you use a combination of the positional $ operator in your projection to return just the items array, use the native JavaScript's map() method to customize the return items array returned for it to have just the fields you want and then since there would be only one array element returned, your final result is obtained by the 0 index position. The following query explains the above:

var item = db.persons.findOne({"items.personId": 1}, {"items.$": 1, "_id": 0}).items.map(function (item){ 
    return { 
        _id: item._id, 
        first_name: item.first_name,
        last_name: item.last_name
    } 
})[0];
printjson(item);

Output:

{
    "_id" : ObjectId("564635a944ae32e021f8b819"),
    "first_name" : "A",
    "last_name" : "B"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. you beat me to it!
1

Have a look at: MongoDB get SubDocument. In short: What you want seems to be impossible. You get something similar with aggergation. The best you can do without aggregation would be:

db.persons.findOne({"items.personId" : 1}, {"items.$", "_id":0})

which would return

{
    "items" : [
        {
            "_id" : ObjectId("564635a944ae32e021f8b819"),
            "first_name" : "A",
            "last_name" : "B"
        }
    ]
}

1 Comment

Hi thanks, this command return all. i want just first_name, last_name and _id of items. and you edit "items.$" to "items.$" : 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.