Home > Mobile >  MongoServerError: unknown group operator '$top'
MongoServerError: unknown group operator '$top'

Time:09-27

I am using mongoose ORM to query my mongodb database. I am facing an issue when using aggregation. I am a novice at mongodb. I am using this pipeline to filter out chats for a particular user for a chatting application.

MongoServerError: unknown group operator '$top'

My aggregation query is as below.

aggregate([
            {
              $match: {
                users: ObjectId("<UserID>")
              }
            },
            {
              $group: {
                _id: "$users",
                out: {
                  $top: {
                    output: {
                      _id: "$_id",
                      content: "$content",
                      fromUser: "$fromUser",
                      toUser: "$toUser",
                      users: "$users",
                      messageRead: "$messageRead",
                      createdAt: "$createdAt"
                    },
                    sortBy: {
                      createdAt: -1
                    }
                  }
                }
              }
            },
            {
              "$replaceRoot": {
                "newRoot": "$out"
              }
            }
          ])

CodePudding user response:

I agree with @nimrod serok in the comments that the actual error described is almost certainly a version mismatch. Check your cluster's configuration in Atlas to confirm that it is on 6.0.

That said, I think you may be able to approach the operation entirely differently. If I'm interpreting the aggregation correctly, the intention is to find a single document with a given value for the users field which was createdAt most recently. If so, then there is no need for the $group and subsequent $replaceRoot at all.

Instead, just consider doing a $sort and then {$limit: 1}. If you additionally have an index on { users: 1, createdAt: -1 } then the operation should be very efficient. Here is a playground demonstrating the approach.

Somewhat relatedly, it might be worth noting that this alternative approach can be handled with a simple find() (with .sort() and .limit()) rather than using the full aggregation framework. Either should be perfectly acceptable.

  • Related