MongoDB shell: check if update succeeded - mongodb

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.

Related

Aggregation not working with monger for mongodb version 3.6

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.

Compatibility issue for mongoDB 3.6 to add Validator

{ "ok" : 0,
"errmsg" : "The featureCompatibilityVersion must be 3.6 to create a collection validator using 3.6 query features. See http://dochub.mongodb.org/core/3.6-feature-compatibility.",
"code" : 224,
"codeName" : "QueryFeatureNotAllowed"
}
The Above is the error in MongoDB.
Version - 3.6.2;
OS: Ubuntu 16.0.4;
Thank you guys for trying to answer my question but I solved the issue myself.
For me the solution was to execute the following command:
db.adminCommand({setFeatureCompatibilityVersion: "3.6"})

Mongo Copy Database Collect Exist Error

I am trying to copy a database from a remote host over to the current database. I am using this command:
rs0:PRIMARY> db.copyDatabase("olddb", "newdb", "xx.xx.xx.137", "user1", "abc123");
But I am getting this error:
{
"done" : true,
"ok" : 0,
"errmsg" : "failed to create collection \"newdb.email_batches\": collection already exists"
}
Except the collect does not exist. What could I be doing wrong in this command?
The problem was different database versions. The DB I was on was Mongo 3.0 and the database I was trying to transfer from was 3.2
What is very strange about that is the command copyDatabase as functionaliy for transferring data from 2.6 to 3.2, yet for some reason does not work with 3.0.

MongoDB not showing collections information even though I am sure its there

So I am using MongoDB 3.2 version.
I created a db and its collection via a Clojure wrapper called monger
But when I connect to the mongo shell, and check if collections are created I can't see it.
Here's the code:
Primary> use db_name
PRIMARY> db.version()
3.2.3
PRIMARY> db.stats()
{
"db" : "db_name",
"collections" : 4,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 16384,
"numExtents" : 0,
"indexes" : 9,
"indexSize" : 36864,
"ok" : 1
}
PRIMARY> show collections
PRIMARY> db.coll1.getIndexes()
[ ]
PRIMARY> db.getCollectionInfos()
Tue May 24 16:29:44 TypeError: db.getCollectionInfos is not a function (shell):1
PRIMARY>
But when I check if collections are created via clojure I can see the information.
user=> (monger.db/get-collection-names mongo-db*)
#{"coll1" "coll2" "coll3" "coll4"}
What is going on?
Found the issue. So it turns out that if the mongo shell and running mongo instance are of two different versions then db.getCollectionNames() and db.collection.getIndexes() will return no output.
This can happen if you are connecting to a remote mongo instance and the instance via you are connecting to is running say 2.x shell version (you can see this when you start the shell) and the running mongo is 3.x version.
According to the documentation:
For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.getCollectionNames() and db.collection.getIndexes() from a version of the mongo shell before 3.0 or a version of the driver prior to 3.0 compatible version, db.getCollectionNames() and db.collection.getIndexes() will return no data, even if there are existing collections and indexes. For more information, see WiredTiger and Driver Version Compatibility.
Spent almost an hour trying to figure this out, thought this might be helpful to others.

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.