1

i've simple schema where Events.like store objectId of user. i'd like to match a particular user in array (Events.like) and add a flag isLiked according to match.

   Events.aggregate([
    { $unwind: '$like'},
      {
          "$project":{
            //   "count" : {$size: "$like"},
            "like":1,
          "isLiked" :{ "$eq": [ "like", "593564b94642650d5b09f16b"  ] },
            //   "isLiked" : { "$cond": [{ "$eq": [ "like", "593564b94642650d5b09f16b" ] }, true, false ] } }
            }
        }
    ], function(err, list) { }

this method always give me false. see results

 "data": [
    {
      "_id": "593647ae9e10082982d3f7a2",
      "like": "593563f66d2e9f0b84553fc3",
      "isLiked": false
    },
    {
      "_id": "593643a5a1e73a2841d3cddb",
      "like": "593564b94642650d5b09f16b",
      "isLiked": false
    },
    {
      "_id": "593643a5a1e73a2841d3cddb",
      "like": "593563f66d2e9f0b84553fc3",
      "isLiked": false
    }
  ]

can anyone tell me where i'm mistaking

########## update ##########

{ $unwind: '$like'},
    {
          "$project":{
          "isLiked" :{ "$eq": [ "$like", mongoose.Types.ObjectId("593564b94642650d5b09f16b")  ] },
        }
    },
    {
        $group: {
          _id: '$_id',
          'likes': { $sum: 1},
          "like": { $push: '$like' },
          "isLiked" : 1
      }
    }

then MongoError: the group aggregate field 'isLiked' must be defined as an expression inside an object

i simple want below result

 "data": [
    {
      "_id": "593647ae9e10082982d3f7a2",
      "like": ["593563f66d2e9f0b84553fc3","593643a5a1e73a2841d3cddb" ],
      "likes":2
      "isLiked": true
    },
    {
      "_id": "593643a5a1e73a2841d3cddb",
      "like": "[593563f66d2e9f0b84553fc3"],
      "likes":1,
      "isLiked": false
    }
  ]
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
shivshankar
  • 2,067
  • 1
  • 18
  • 28
  • It's an `ObjectId` and not a "string" You should be doing `{ "$eq": [ "like", ObjectId("593564b94642650d5b09f16b") ] }` – Neil Lunn Jun 06 '17 at 13:45
  • 1
    Duplicate of [Moongoose aggregate $match does not match id's](https://stackoverflow.com/questions/36193289/moongoose-aggregate-match-does-not-match-ids) – Neil Lunn Jun 06 '17 at 13:47

1 Answers1

1

Hi the user id you are passing is being passed as a string you will need to convert it to objectId. Try this

EDIT: fixed path of the field as pointed by the asker, @ShivShanker

Events.aggregate([
{ $unwind: '$like'},
  {
      "$project":{
        //   "count" : {$size: "$like"},
        "like":1,
      "isLiked" :{ "$eq": [ "$like", mongoose.Types.ObjectId("593564b94642650d5b09f16b")  ] }
        }
    }
], function(err, list) { }
Samip Suwal
  • 1,253
  • 11
  • 17
  • thanks samip but works only when $like insted on like. modified line as "isLiked" :{ "$eq": [ "$like", mongoose.Types.ObjectId("593564b94642650d5b09f16b") ] }, – shivshankar Jun 06 '17 at 14:00
  • oh right I only noticed objectId issue, didn't notice that the path was wrong, good catch. – Samip Suwal Jun 06 '17 at 14:01
  • can you pls give me hint how to collect unwinded results again using $group and $push. it's giving different issues to me – shivshankar Jun 06 '17 at 14:09
  • without looking at the schema (or a document sample), or knowing what exactly you are trying to achieve, and what issue you are facing. Its little hard to answer this. Also, it might be better if you post a second question related to issues you are having, as it seems this is a separate issue. Do take a look at this though https://stackoverflow.com/questions/30119575/group-array-after-unwind-and-match maybe this will serve as a hint. – Samip Suwal Jun 06 '17 at 14:19
  • sounds to me this is what all you need Events.aggregate({ "$project":{ "like":1, "likes": {"$size": "$like"}, "isLiked" :{ "$in": [ ObjectId("593643a5a1e73a2841d3cddc"), "$like" ]}} }) Since all you need to do is check if an user liked the event or not, and return the "like" array and number of likes. – Samip Suwal Jun 06 '17 at 15:45