4

Say this is one item from my users collection:

  {
    "_id" : "5545f4c4d0dd52c355a99fbe",
    "name" : "Dollie James",
    "device" : "iOS",
    "gender" : "Female",
    "wallet" : [
        {
            "store" : "All Saints",
            "balance" : "$196.11",
            "date" : "2014-02-22T22:09:38 -10:00",
            "tags" : [
                "Tshirt",
                "Summer"
            ]
        },
        {
            "store" : "Nike",
            "balance" : "$367.76",
            "date" : "2014-04-18T14:44:30 -10:00",
            "tags" : [
                "Shoes"
            ]
        }
    ]
}

This record was returned from the following query:

db.users.findOne({$and:[{"wallet.tags" : "Tshirt"}, {"wallet.store":"Nike"}]})

However this is not the result I want because Tshirt and Nike are not in the same object in the wallet array. It seems that mongo is doing a query on the entire array. How can I target this query to only return my two conditions that are in the same object within the array?

4
  • have you tried $and operator? Commented May 3, 2015 at 11:13
  • yes, I get the same result Commented May 3, 2015 at 11:13
  • you can use $and operator see here docs.mongodb.org/manual/reference/operator/query/and Commented May 3, 2015 at 11:14
  • edited. $and operator returns the same result Commented May 3, 2015 at 11:17

1 Answer 1

9

You don't need $and (as it will apply conditions independently) but $elemMatch. From the doc:

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

In your specific case:

> db.wallet.find(
              {wallet: { $elemMatch:{"tags" : "Tshirt", "store":"Nike"}}}
  ).pretty()
{
    "_id" : "5545f4c4d0dd52c355a99f00",
    "name" : "Working example",
    "device" : "iOS",
    "gender" : "Female",
    "wallet" : [
        {
            "store" : "Nike",
            "balance" : "$196.11",
            "date" : "2014-02-22T22:09:38 -10:00",
            "tags" : [
                "Tshirt",
                "Summer"
            ]
        }
    ]
}
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.