Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isssue withe agregate method. #20

Open
vcailly opened this issue Feb 2, 2018 · 4 comments
Open

Isssue withe agregate method. #20

vcailly opened this issue Feb 2, 2018 · 4 comments

Comments

@vcailly
Copy link

vcailly commented Feb 2, 2018

To reproduce the problem, you have just to follow the tutorial at : https://www.codeproject.com/Articles/1096142/MongoDB-Tutorial-Day-Aggregation

**1 Create the collection **

db.Student.insert({StudentName : "Vijay",Section : "A",Marks:70,Subject:["Hindi","English","Math"]})
db.Student.insert({StudentName : "Gaurav",Section : "A",Marks:90,Subject:["English"]})
db.Student.insert({StudentName : "Ajay",Section : "A",Marks:70,Subject:["Math"]})
db.Student.insert({StudentName : "Ankur",Section : "B",Marks:10,Subject:["Hindi"]})
db.Student.insert({StudentName : "Sunil",Section : "B",Marks:70,Subject:["Math"]})
db.Student.insert({StudentName : "Preeti",Section : "C",Marks:80,Subject:["Hindi","English"]})
db.Student.insert({StudentName : "Anuj",Section : "C",Marks:50,Subject:["English"]})
db.Student.insert({StudentName : "Palka",Section : "D",Marks:40,Subject:["Math"]})
db.Student.insert({StudentName : "Soniya",Section : "D",Marks:20,Subject:["English","Math"]})

**2 Run the following agregate from Node-red **

db.Student.aggregate ([
{
"$group":
{
"_id":
{
"Section" : "$Section"
},
"TotalMarks":
{
"$sum": "$Marks"
},
"Count":{ "$sum" : 1},
"Average" : {"$avg" : "$Marks"}
}
},
{
"$project" :
{
"SectionName" : "$_id.Section",
"Total" : "$TotalMarks"
}
},
{
"$sort":{"SectionName":-1}
},
{
"$limit" : 2
}
])

**3 if we use the mongodb node we have the good result : an Array of 2 entries **

[{"_id":{"Section":"D"},"SectionName":"D","Total":60},{"_id":{"Section":"C"},"SectionName":"C","Total":130}]

**4 if we use the mongodb2 node we do not have the good result : an Array of 4 entries. It looks like having the $limit operator ignored **

[{"_id":{"Section":"D"},"SectionName":"D","Total":60},{"_id":{"Section":"C"},"SectionName":"C","Total":130},{"_id":{"Section":"B"},"SectionName":"B","Total":80},{"_id":{"Section":"A"},"SectionName":"A","Total":230}]

@ozomer
Copy link
Owner

ozomer commented Feb 11, 2018

The mongodb2 simply executes the mongodb command.
Please try wrapping your parameters with an array, i.e. doing msg.payload = [...] or even msg.payload = [[...]].

@s4ke
Copy link

s4ke commented Nov 9, 2020

I can confirm this behaviour. It seems like the last part of any given pipeline is ignored. We opted to simply work around this by including an identity projection in the end.

@s4ke
Copy link

s4ke commented Nov 18, 2020

Coming back to this: As @ozomer pointed out msg.payload = [[ ]] should do the trick for aggregate.

This is due to this code:

                if ((operation.length > 0) && (args.length > operation.length - 1)) {
                    // The operation was defined with arguments, thus it may not
                    // assume that the last argument is the callback.
                    // We must not pass too many arguments to the operation.
                    args = args.slice(0, operation.length - 1);
                }

@jomel
Copy link
Contributor

jomel commented Nov 19, 2020

The docs in this repo Quite clearly state: _"If you want to pass a single parameter WHICH IS AN ARRAY (eg as with InserMany), wrap your array in an outer array: msg.payload = [[{_id: 1243}, {id: 2345}]]"

This issue should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants