[DOCS-12044] Aggregation lookup example is not working on mongodb version: 4.0.2 Created: 10/Sep/18  Updated: 27/Oct/23  Resolved: 11/Sep/18

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: 4.0.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Jorge Soares Assignee: Nicholas Larew
Resolution: Works as Designed Votes: 0
Labels: debian, docker, jessie
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Debian Jessie


Issue Links:
Duplicate
is duplicated by DOCS-12045 $graphLookup examples should include ... Closed
Related
related to DOCS-12036 Aggregation examples don't work due t... Closed
Participants:
Days since reply: 5 years, 22 weeks, 2 days ago

 Description   

Description

The example:

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#perform-a-single-equality-join-with-lookuphttps://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#pipe._S_lookup

Gives a parsing error:

 Error: Printing Stack Trace
 at printStackTrace (src/mongo/shell/utils.js:37:15)
 at DBCollection.aggregate (src/mongo/shell/collection.js:897:9)
 at (shell):1:11
Mon Sep 10 19:27:07.199 aggregate failed: {
 "ok" : 0,
 "errmsg" : "The 'cursor' option is required, except for aggregate with the explain argument",
 "code" : 9,
 "codeName" : "FailedToParse"
} at src/mongo/shell/collection.js:898

Is the documentation correct?

I copied the example word for word after getting the same errors on a data structure I was creating myself.

Scope of changes

Impact to Other Docs

MVP (Work and Date)

Resources (Scope or Design Docs, Invision, etc.)



 Comments   
Comment by Nicholas Larew [ 11/Sep/18 ]

Jorge,

I think the issue you're encountering stems from running an old version of the mongo shell. The shell is released as a component of the mongodb server distribution, and it mirrors the version number of the server. In general you should always use a shell with at least the same version as the server you are connecting to.

The cursor behavior you're encountering was added in version 3.6, so the 2.4.10 shell cannot handle the command correctly. In your case, you should upgrade to shell version 4.0.2, which you'll find included in the bin directory of the current stable Community Server release.

 

I'm going to close this ticket as "Works as Designed". If you continue having issues after upgrading to the latest version of the mongo shell, feel free to comment on this ticket or file a new one.

Comment by Jorge Soares [ 11/Sep/18 ]

Nick,

Mongo Shell Version - MongoDB shell version: 2.4.10

MongoDB server - 

> db.version()
4.0.2

I got the mongodb image from:

https://hub.docker.com/_/mongo/

I am running the docker container under vagrant, OS:

vagrant@ircbot1:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.11 (jessie)
Release: 8.11
Codename: jessie

I had tried your suggestion before, with the cursor attribute set, but I get a parsing error as well.

It's definitely a problem at my end.

I reckon will create a pristine vagrant VM and run the steps again.

Thanks for having a look.

 

Comment by Nicholas Larew [ 10/Sep/18 ]

Jorge,

I'm not entirely sure what the root of the issue is, particularly since you didn't see any results from trying --norc. I'll make sure we investigate what's happening here as best as we can. Could you provide some information about your environment to help us? Specifically:

  • What version of the mongo shell are you running? (You can find out by running mongo --version)
  • What version of the MongoDB server are you running / connecting to?

In the meantime, you should be able to circumvent this issue by specifying the cursor option in the aggregation options document (the second argument to the aggregate method), e.g.

db.orders.aggregate([
  { $lookup: {
        from: "inventory",
        localField: "item",
        foreignField: "sku",
        as: "inventory_docs"
  } }
], {
  cursor: {}
})

Comment by Jorge Soares [ 10/Sep/18 ]

Hi Nick.

 

Thanks for your prompt reply.

Do you mean:

vagrant@ircbot1:~$ mongo --norc localhost/recStore 

I ran it.

Changed nothing out of this example:

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#perform-a-single-equality-join-with-lookup

And I still get:

 > db.orders.remove({})
> db.inventory.remove({})
> db.orders.find()
> db.inventory.find()
> db.orders.insert([
...    { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
...    { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
...    { "_id" : 3  }
... ])
> db.orders.find()
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }
{ "_id" : 3 }
> db.inventory.insert([
...    { "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
...    { "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
...    { "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
...    { "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
...    { "_id" : 5, "sku": null, description: "Incomplete" },
...    { "_id" : 6 }
... ])
> db.inventory.find()
{ "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 }
{ "_id" : 2, "sku" : "bread", "description" : "product 2", "instock" : 80 }
{ "_id" : 3, "sku" : "cashews", "description" : "product 3", "instock" : 60 }
{ "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 }
{ "_id" : 5, "sku" : null, "description" : "Incomplete" }
{ "_id" : 6 }
> db.orders.aggregate([
...    {
...      $lookup:
...        {
...          from: "inventory",
...          localField: "item",
...          foreignField: "sku",
...          as: "inventory_docs"
...        }
...   }
... ])
Error: Printing Stack Trace
    at printStackTrace (src/mongo/shell/utils.js:37:15)
    at DBCollection.aggregate (src/mongo/shell/collection.js:897:9)
    at (shell):1:11
Mon Sep 10 22:12:40.866 aggregate failed: {
        "ok" : 0,
        "errmsg" : "The 'cursor' option is required, except for aggregate with the explain argument",
        "code" : 9,
        "codeName" : "FailedToParse"
} at src/mongo/shell/collection.js:898

Not sure if I'm doing something wrong. Very new to mongodb...

Regards,

Jorge

Comment by Nicholas Larew [ 10/Sep/18 ]

Hi Jorge,

Thanks for filing a docs ticket! The examples listed on the page match the expected behavior of the commands.

The error you posted should only be output when the cursor option is not set in the aggregation command (as of version 3.6), i.e. db.runCommand({ aggregate: "collection", pipeline: [ ... ], cursor: {} }). No examples on the $lookup page use this form.

The aggregation shell helper, i.e. db.collection.aggregate([ ... ]), automatically returns a cursor.

 
This might be an issue with a shell extension that is overwriting the default aggregate() shell helper method. Try running the shell with the --norc flag and see if the issue persists.

 

Generated at Thu Feb 08 08:04:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.