Combine slice and filter projection mongodb

Viewed 51

Hope you can help me with this one.

I am struggling with my projection stage of my query. I have two instances that both work, and somehow I am not able to combine them:

{ $project : { "shortList": { "$slice": [ "$shortList", 3 ], } } },

AND

{ $project : { _id : 0, "shortList._id": 0, "shortList.listTitle": 0 } },

Independently work, however not in combination. I tried for example:

{ $project : { "shortList": { "$slice": [ "$shortList", 3 ], }, _id : 0, "shortList._id": 0, "shortList.listTitle": 0 } },

and then afterwards received the error:

"errmsg" : "Invalid $project :: caused by :: Path collision at shortList._id remaining portion _id",

Which is also mentioned here: Mongodb get error message "MongoError: Path collision at activity"

However I don't get it completely, how can I make this work?

My total query is the following:

         { $sort : { "sublist.subListAdded" : -1} },
         { $limit : 3 },
         { $unwind: "$sublist"},
         { $unwind: "$sublist.item"},
         { $unwind: "$sublist.item.itemTitle"},
      
         {
       $group:
         {
            "_id": {
                        "itemTitleTest":  "$sublist.item.itemTitle",
                        "listTitleTest": "$listTitle"
                  },
            listTitle:  { $first: "$listTitle"},
            chapters:  { "$first": "$chapters"},
            itemTitle:  { "$first": "$sublist.item.itemTitle"},
            totalScore: { $sum: "$sublist.item.itemScore" },
            count: { $sum: 1 },
            itemComments: { $push:  {  itemCommentCreator: "$sublist.subListCreator", itemCommentText: "$sublist.item.itemContext" } }
         }, },

         { $sort : { listTitle: -1, totalScore : -1, count: -1} },
         { $group:
         {
            _id:  "$listTitle", 
            listTitle:  { "$first": "$listTitle"},
            shortList: { $push: "$$ROOT" }
         },
     },

        { $project : {  
           "shortList": { "$slice": [ "$shortList", 3 ],  }
  } },

])

Thanks a lot for helping!!

0 Answers
Related