jones
jones

Reputation: 1453

Mongodb how to add a custom field in projection clause if an array of ojects contain specifc value

Suppose I have a company collection, each post document may have a field followers, which is an array of objects, like this:

{
    "_id" : ObjectId("5a82831c0b74a9276c3f826c"),
    "updatedAt" : ISODate("2018-04-07T11:42:15.490Z"),
    "createdAt" : ISODate("2018-02-13T06:18:04.630Z"),
    "name" : "MY First post",
    "description" : "One the best IT solution company",
    "follows" : [ 
        {
            "userId" : ObjectId("5a82b527fe741c1854fcabe8"),
            "_id" : ObjectId("5ac8ae9724f44d7e88d8d7f0")
        },
        {

           "userId" : ObjectId("5a82831c0b74a9276c3f826b"),
           "_id" : ObjectId("5ac8aa818e932c05e0bc4af3")
        }
    ],
    "likes" : [],

    "owner" : {
        "id" : ObjectId("5a82831c0b74a9276c3f826b"),
        "firstName" : "User",
        "lastName" : "One"
    },
    "createdBy" : {
        "id" : ObjectId("5a82831c0b74a9276c3f826b"),
        "firstName" : "User",
        "lastName" : "One"
    },
    "status" : 1
}

{
    "_id" : ObjectId("5a939bce456ea36f18de80ad"),
    "updatedAt" : ISODate("2018-04-07T11:51:09.407Z"),
    "createdAt" : ISODate("2018-02-26T05:31:58.918Z"),
    "name" : "MY second post",
    "description" : "Afghanistan Development Registry And Services is a co-governance company.",

    "follows" : [],
    "likes" : [],
    "owner" : {
        "id" : ObjectId("5a939bce456ea36f18de80ac"),
        "firstName" : "Aslam",
        "lastName" : "Popal"
    },
    "createdBy" : {
        "id" : ObjectId("5a939bce456ea36f18de80ac"),
        "firstName" : "Aslam",
        "lastName" : "Popal"
    },
    "status" : 1
}

Now I have the following query:

db.getCollection('companies').aggregate([
  {"$match":{"status":1}},
  {"$project":{
      "follows":1,"benefits":1,"totalRatingAverage":1,
      "reviews":{"$size":{"$ifNull":["$reviews",[]]}},
      "isInMyFollowing": {$eq: ["$follows.userId", ObjectId("5a82b527fe741c1854fcabe8")]}
  }},
  {"$sort":{"name":-1}},
  {"$skip":0},{"$limit":10}
])

Now I want to add a field to my selection result to show is each company followed by me or no?

I have updated my data sample, please check again.

Upvotes: 0

Views: 73

Answers (1)

Rahul Raj
Rahul Raj

Reputation: 3459

After a close check, found that your $follows.userId actually returns an array instead of ObjectId("5a82b527fe741c1854fcabe8").

"test" : [ ObjectId("5a82b527fe741c1854fcabe8") ]

Update: Try this query that compares with right value of ObjectId:

db.getCollection('sample').aggregate([
 {"$match":{"status":1}},
 {"$project":{
  "follows":1,"benefits":1,"totalRatingAverage":1,
  "reviews":{"$size":{"$ifNull":["$reviews",[]]}},
  "isInMyFollowing": {$in: 
 [ObjectId("5a82b527fe741c1854fcabe8"),"$follows.userId"]}
 }},
 {"$sort":{"name":-1}},
 {"$skip":0},{"$limit":10}
])

And it gives me output:

/* 1 */
{
"_id" : ObjectId("5a82831c0b74a9276c3f826c"),
"follows" : [ 
    {
        "userId" : ObjectId("5a82b527fe741c1854fcabe8"),
        "_id" : ObjectId("5ac8ae9724f44d7e88d8d7f0")
    }, 
    {
        "userId" : ObjectId("5a82831c0b74a9276c3f826b"),
        "_id" : ObjectId("5ac8aa818e932c05e0bc4af3")
    }
],
"reviews" : 0,
"isInMyFollowing" : true
}

/* 2 */
{
"_id" : ObjectId("5a939bce456ea36f18de80ad"),
"follows" : [],
"reviews" : 0,
"isInMyFollowing" : false
}

Upvotes: 1

Related Questions