NoobSter
NoobSter

Reputation: 1160

How to $match by _id provided in $project with mongodb aggregate?

I'm wanting to be able to match / filter for a specific style from whiskey.style.

I'm wondering if it's not matching due to the formatting of the OID. I tried toString() as the documentation seems to suggest - may need to investigate this more..

Here is my query / $match object

var qObj.whiskeyFilter = { whiskey: { style: '57953144abfaa62383341a72' },
      _id:
       { '$in':
          [ 57a115304d124a4d1ad12d81,
            57a114d64d124a4d1ad12d7f,
            57a1152a4d124a4d1ad12d80,
            57a9049906f3733623826538 ] } }

my pipeline:

  var pipeline = [
            {
             "$project": {
               "weight": stack[0],
               "whiskey": "$$ROOT",
               "collection":
                 collect[0]
             }
           },
           {
            "$match": qObj.whiskeyFilter
          }, {
            "$sort": {
              "weight": 1
            }
          }, {
            "$limit": 12
          }, {
            "$skip": qObj.skip
          }];

this works if I only include the _id / $in for the $match, but it will not $match with whiskey.style.

Here is an example of what would return from the aggregate:

[ { _id: 57a115304d124a4d1ad12d81,
    weight: 1,
    whiskey:
     { _id: 57a115304d124a4d1ad12d81,
       name: 'sample whiskey 2',
       distiller: 578c04f76091bcd618f26e04,
       style: 57953144abfaa62383341a72,
       userVote: 0,
       timestamp: Tue Aug 02 2016 16:48:32 GMT-0500 (CDT),
       total: 1,
       vote: 2,
       __v: 0 },
    collection:
     { _id: 57acb4ff093360bee276aae6,
       user: 57919ac16fa0390856a9998f,
       whiskey: 57a115304d124a4d1ad12d81,
       __v: 0,
       collected: true,
       vote: 1,
       timestamp: Thu Aug 11 2016 12:25:19 GMT-0500 (CDT),
       favorite: true } }
]

Update

I'm converting it into an objectId, as i've read the aggregation may have issues casting to string, but I still am not getting any returns for an expected match:

mongoose.Types.ObjectId(obj.style);

now, you can see that the style Id is no longer a string, but still $match does not seem to work:

match query :  { whiskey: { style: 57953144abfaa62383341a72 },
  _id:
   { '$in':
      [ 57a115304d124a4d1ad12d81,
        57a114d64d124a4d1ad12d7f,
        57a1152a4d124a4d1ad12d80,
        57a9049906f3733623826538 ] } }

Upvotes: 2

Views: 953

Answers (1)

NoobSter
NoobSter

Reputation: 1160

Figured it out:

I had to modify and add my style to filter by in the main object / not nested

    {
     "$project": {
       "weight": stack[0],
       "whiskey": "$$ROOT",
       "style": "$style",  <--- added
       "collection":
         collect[0]
     }

For some reason, it was not capable of filtering the nested $$ROOT object.

I'll add more detail if I can find it in the docs. Or, if anyone else would like to expand on the answer -- i'd prefer to accept that than my own.

Upvotes: 1

Related Questions