Aggregation not working with monger for mongodb version 3.6 - mongodb

Mongo aggregation framework has some changes in version 3.6 Earlier aggregation queries with monger are not working even when we pass :cursor {} as an option. Is there any workaround or do we have to wait for the next monger release?. The error we get is specified below
MongoCommandException Command failed with error 9: 'The 'cursor' option is required, except for aggregate with the explain argument' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "The 'cursor' option is required, except for aggregate with the explain argument", "code" : 9, "codeName" : "FailedToParse" } com.mongodb.connection.ProtocolHelper.getCommandFailureException (ProtocolHelper.java:115)

by OSt advice, I could run monger aggregation sample with codes below.
(mc/aggregate db coll
[{"$project" {:subtotal {"$multiply" ["$quantity", "$price"]}
:_id "$state"}}]
:cursor {:batch-size 0})
thanks!

According to mongo db spec, cursor became a required field in some cases. So you should provide it through monger API. It is not a problem in monger, it is a breakable change in mongo db API.

Related

Query a stored procedure in mongodb using spring boot

I want to query a stored procedure in mongodb. I can query it using the command line tool but facing issue while querying using java.The piece of code(last two lines) that is throwing error is :
MongoClient mongoClient = new MongoClient();
MongoDatabase mdb = mongoClient.getDatabase("mydb");
mdb.runCommand(new Document("$eval", "db.loadServerScripts()"));
Document doc1 = mdb.runCommand(new Document("$eval", "mysp(5)"));
and the error that it's throwing is 'no such command: '$eval'' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "no such command: '$eval'", "code": 59, "codeName": "CommandNotFound"}
Now I read several posts and documentation as well stating that $eval or db.eval() doesn't works for mongo version 4.2. So what should I change in my code to make it work or what should be the possible solution. I know this question has been asked several times but those solutions are obsolete , so I need help for this. Can anyone help.
From mongodb-4.2, the support for the eval command is deprecated now.
So I guess the only option will be to use mongodb-4.0 as of now.
Source : https://docs.mongodb.com/manual/reference/method/db.eval/

Unrecognized pipeline stage name: '$sample'

when I run this aggregation pipeline in Robomongo
db.getCollection('xyz').aggregate([{$match: {tyu: "asd", ghj: "qwe"}},
{$sample: {size: 5}}])
I receive this error:
assert: command failed: {
"errmsg" : "exception: Unrecognized pipeline stage name: '$sample'",
"code" : 16436,
"ok" : 0
I'm using mongodb ver 3.2.6 and since $sample is supported from 3.2 onward.
(https://docs.mongodb.com/manual/reference/operator/aggregation/sample/#pipe._S_sample)
Im a little confused as to why I receive this error message.
Maybe I'm just missing something small.
Thanks
As stated in the comments of the question. Mongo client had a version of 3.2.6 but Mongo db had a version of 3.0.6.
I used version() in the shell to get the client's version and
db.version() to get the DB's version.
ver 3.0.6 is too low to support $sample as stated in the mongo documentation
https://docs.mongodb.com/manual/reference/operator/aggregation/sample/#pipe._S_sample

Text search on mongodb 2.6 returning error

I'm using mongoDB 2.6 and the structure of my document is something like this:
{
"about": <about me>,
"_id" : <id>
}
I want to apply text search on about field. I did it using: db.user.ensureIndex({"about":"text"})
But when I run a search query: db.user.find({$text:{$search:"internet"}}) I get this error: error: { "$err" : "invalid operator: $search", "code" : 10068 } As per the documentation, text search is enabled on mongodb 2.6 by default. So what could be wrong? Earlier I had 2.2 installed on my machine. I recently downloaded 2.6 and I applied this index using 2.6 only.

mongodb 2.4.1 failing with 'process out of memory'

I have the following mongo versions
db version v2.4.1
MongoDB shell version: 2.4.1,
and
db version v2.2.1-rc1, pdfile version 4.5,
MongoDB shell version: 2.2.1-rc1
installed on 64-bit windows 7 machine.
I have a collection having 10001000 (10 million+) records, when I use V 2.4.1 to aggregate, it fails with the following
error:
Fatal error in CALL_AND_RETRY_2
Allocation failed - process out of memory
However when I use V 2.2.1-rc1, to aggregate the same collection, it works fine and gives result in around 1 minute.
Sample document of the collection that is being aggregated:
{
"_id" : ObjectId("516bdd1c39b10c722792e007"),
"f1" : 10000010,
"f2" : 10000000,
"key" : 0
}
Aggregation Command:
{$group: {"_id": "$key", total: {$sum: "$f1"}}}
Command used to populate records:
for(var i = 10011000; i < 10041000; ++i)
{
db.testp.insert({"f1": i+10, "f2": i, "key": i%1000})
}
How much memory do you have? Could it be the $group is taking up more than 10% of available memory and causing the error? See the aggregation documentation on memory for cumulative operators.
edit 1:
Out of interest - does the aggregation work outside the shell? eg calling it from a driver.
I have seen similar v8 errors and as the shell was updated to v8 in 2.4 Theres a chance it could be that.
edit 2:
If the resulting array is too big in the shell then that can also trigger the error: see SERVER-8859. To work around you might need to run multiple aggregations, either by doing a $match early on to limit the working set or even a $skip and $limit to paginate through the result set.
I tried your aggregation with 10,070,999 docs on 2.4.1 on a mac and didn't get the error

MongoDB shell: check if update succeeded

Similar to MongoDB update: how to check if an update succeeds or fails? but for default mongodb shell. db.collection.update() will execute silently in both cases: when query has found a document and when not. And getLastError is also null after both updates.
How can I find out that something was actually updated without re-querying collection?
I am using MongoDB version 2.0.4 on Ubuntu 12.04
The db.getLastErrorObj() is what you want to call to get the result of the update. It returns an object that looks like:
{
"updatedExisting" : true,
"n" : 2,
"connectionId" : 35,
"err" : null,
"ok" : 1
}
n is the number of updated documents.