0

I have a simple mongodb collection (an extract of aggregation pipeline), say

[
 {_id : [3, 3] ,  val : 7},
 {_id : [3, 2] ,  val : 9},
 {_id : [1, 3] ,  val: 10}
]

I want to do a $match to get the [3, 3] record only. However using

$match : {$_id: { $all: [3, 3]}}

returns [3, 2] as well, which is what we don't want. Any takers?

3
  • Yup. Complete lie, as _id cannot be an array. Commented Sep 29, 2015 at 14:19
  • 1
    i think you missed the part of my question saying that its an extract of aggregation pipe line, i can be an array thus Commented Sep 29, 2015 at 14:23
  • I think you missed the part of your question to show your workings and proof. Commented Sep 29, 2015 at 14:23

1 Answer 1

2

I can only see this possibly occurring under a condition like this:

{ "like": 1, "id": 3, "val": 7 },
{ "like": 1, "id": 3, "val": 7 },
{ "like": 2, "id": 3, "val": 5 },
{ "like": 2, "id": 2, "val": 5 },

And then in aggregate:

db.collection.aggregate([
    { "$group": {
        "_id": "$like",
        "id": { "$push": "$id" },
        "val": { "$max": "$val" }
    }},
    { "$project": {
        "_id": "$id",
        "val": 1
    }}
])

Which gives:

{ "_id" : [ 3, 2 ], "val" : 5 }
{ "_id" : [ 3, 3 ], "val" : 7 }

For which we come down to, the $all condition is basically considered as a "set" just as $in is. And that means [3,3] is basically reduced to 3. Therefore if you want an "explicit" match then please do so:

db.collection.aggregate([
    { "$group": {
        "_id": "$like",
        "id": { "$push": "$id" },
        "val": { "$max": "$val" }
    }},
    { "$project": {
        "_id": "$id",
        "val": 1
    }},
    { "$match": {
        "_id": [3,3]
    }}
])
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.