0

I have this documents:

//document 1
{
    info : [
        {
            id  : 100,
            field : {                   
                a : 1,
                b : 2
            }
        },
        {
            id  : 200,
            field : {                   
                a : 3,
                b : 4
            }
        },
        {
            id  : 300,
            field : {                   
                a : 5,
                b : 6
            }
        }
    ]
},
//document 2
{
    info : [
        {
            id  : 400,
            field : {                   
                a : 7,
                b : 8
            }
        },
        {
            id  : 500,
            field : {                   
                a : 9,
                b : 10
            }
        }
    ]
}

I need to find the id of the subdocument with the values field.a = 7 and field.b = 8 , that means the id value is 400.

What i have tried is $elemMatch but I can't get the result.

My attemps :

attemp 1:

db.mycollection.findOne({info : {$elemMatch : {  'field.$.a':7,'field.$.b':8   } } });

attemp 2:

db.mycollection.findOne({info:{$elemMatch:{$elemMatch:{'field.$.a':7,'field.$.b':8,}}}});

attemp 3:

db.mycollection.findOne({info:{$elemMatch:{$elemMatch:{'field.a.$':7,'field.b.$':8,}}}});

attemp 4:

db.mycollection.findOne({info:{$elemMatch:{'field.$.a':7,'field.$.b':8,}}});
3
  • you'll have to use the aggregation framework to get the subdocument's id Commented Nov 18, 2015 at 21:04
  • try db.mycollection.findOne({info : {$elemMatch : {field:{ 'a':7,'b':8 }} } }); Commented Nov 18, 2015 at 21:04
  • this actually works, i just have tu use projection db.mycollection.findOne({info : {$elemMatch : {field:{ 'a':7,'b':8 }} } }, { id:1, info : {$elemMatch : {field:{ 'a':7,'b':8 }} } });, thanks @hecnabae Commented Nov 18, 2015 at 21:24

1 Answer 1

2

The $elemMatch operator works like a "mini query" against the specified array element it is acting on, so arguments go inside. Also the positional $ operator here is a propery of "projection" and not the query document itself, so this is a separate element:

db.mycollection.find(
    {
        "info": {
            "$elemMatch": { "field.a": 7 , "field.b": 8 }
        }
    },
    { "info.$": 1 }
)

Which both matches the document containing the matched element, and then only returns the matched element due to the projection:

{
        "_id" : ObjectId("564d52979f28c6e0feabceee"),
        "info" : [
                {
                        "id" : 400,
                        "field" : {
                                "a" : 7,
                                "b" : 8
                        }
                }
        ]
}
Sign up to request clarification or add additional context in comments.

Comments

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.