New 7.0.6 bug: Limit code returned unexpected value

I noticed a new bug with the MongoDB Community Server 7.0.6 release (Ubuntu Linux), using $sort in an aggregation. I did not experience this bug with 7.0.5.

Code to reproduce the error:

db.foo.insert({x:1, y: 2})
db.bar.insert({z: 2})

db.foo.aggregate(
{$sort:{"x": 1}},
{$skip: 2},
{$limit: 20},
{$lookup:{
	from: "bar",
	localField: "y",
	foreignField: "z",
	as: "xx",
}}
)

Running the above in mongosh gives the following error:

MongoServerError[Location8349205]: Limit code returned unexpected value

3 Likes

If you think you’ve found a bug you should file an issue against CORE SERVER.

Thanks for guiding me. Filed an issue here: https://jira.mongodb.org/browse/SERVER-87324

2 Likes

Nice issue … one can see you have done this before :slight_smile:

This is causing a major issue for us right now in production. We are using mongo atlas, and our version was updated to 7.0.6, which it seems, introduced the error.

Open a support case. Reference the JIRA above and this thread.

Relying on MongoDB employees browsing the forums is not going to escalate the issue, particularly on a weekend.

I opened a case and referred to this post. It was resolved promptly.

1 Like

I already opened a ticket but it has no response. Can you link your issue too?

@Andras_Belicza The other poster is a MongoDB Atlas user. They required MongoDB support assistance, basic support is provided on all tiers.

This is affecting us as atlas customer as well. Cluster automatically upgraded to 7.0.6, no way back.
Thanks for creating an issue @Andras_Belicza. Hope this gets resolved and pushed to all atlas clusters asap as I currently am unable to create a workaround.

Hoping and wishing won’t achieve much, open a support ticket in Atlas.

@Andras_Belicza one way we can still use the pipeline with above stage is when we introduce addFields between limit and lookup stages.
Its a temporary way untill we get a fix from mongodb side

db.foo.aggregate(
{$sort:{"x": 1}},
{$skip: 2},
{$limit: 20},
{$addFields:{v:0}},
{$lookup:{
	from: "bar",
	localField: "y",
	foreignField: "z",
	as: "xx",
}}
)

hope this helps

5 Likes

Nice workaround, thanks for sharing.

I encountered the same issue, but after indexing the field used for sorting in the desired order, my problem was resolved. earlier I got this error:
“PlanExecutor error during aggregation :: caused by :: Limit code returned unexpected value”

db.ReturnRequest.aggregate([
   {
      "$lookup":{
         "from":"Order",
         "localField":"OrderId",
         "foreignField":"_id",
         "as":"Orders"
      }
   },
   {
      "$sort":{
         "CreatedOnUtc":-1
      }
   },
   {
      "$match":{
         "Deleted":false
      }
   },
   {
      "$skip":25
   },
   {
      "$limit":25
   }
])

The error is triggered when aggregating only if $lookup or $group comes after a $limit/$skip/$sort. You can read more about the details here. https://jira.mongodb.org/browse/SERVER-87313

It has already been fixed and should be released soon with 7.0.7

1 Like