Collection.count() on empty collection in Mongodb - mongodb

Collection.count() on empty collection in Mongodb is getting hanged and not returning anything
I am using Mongo java driver 3.2 . It is working from CLI.
Any help will be appreciated.

Related

MongoDB count() query returns outdated result in mongo shell

Documents in my MongoDB collection are changing status field very frequently. I can see it very clear, using a mongoDB client (Robo 3T).
Now I'd like to monitor this process using mongo shell:
mongo --host=localhost db --eval "db.getCollection('events').find({status:'ACTIVE'}).count()"
This returns a correct result, but then mongoDB "caches" it and would not return an updated result for another ~ 10 seconds. I need to have an update every 200 ms.
The same query from Robo 3T always returns an updated result, in ~5ms.
From my observation, when the load is lower, mongo shell count updates are coming on every request.
I can't find any cache-like mechanism information in MongoDB documentation. How can I disable it? Why does it work ok from Robo 3T?
P.S
I observed the same behavior with a python script, which is polling mongo with count() query - the results are cached. But once I start execute a query in Robo 3T, the numbers start moving in python and mongo shell! What is happening?
Use itcount() which actually executes the query on an existing iterator.
mongo --host=localhost db \
--eval "db.getCollection('events').find({status:'ACTIVE'}).itcount()"
Give a try to remove all the cached query plans for a collection:
db.collection.getPlanCache().clear()
if you don’t change the where condition, updates will be cached.
Doc: Plan cache

mongodb - command 'show collection' doesn't show anything

I switched my db properly and also confirmed that there is a document in my collection but I cannot find any collection by 'show collection' command.
Can I know the reason?
Please check your Mongo shell version and MongoDB version are compatible.
Refer to this JIRA ticket which has the similar issue reported. The root cause of the problem was the Mongo shell version and MongoDB version were not compatible.
To get Mongo Shell version:-
Go to the bin folder and execute mongo command which will print the Mongo shell version in the first line.
To get MongoDB version:-
db.version()

Java MongoDB driver can't parse query

I'm developing project with hibernate-ogm 5 with mongodb 3. But some query can not parsed. But I tested this query on shell. It works. What's wrong with this query?
com.mongodb.util.JSONParseException:
db.Tree.update({'_id':2},{'$inc':{'totalUserCount':NumberInt(-1)}},{})
^
com.mongodb.util.JSONParser.parse(JSON.java:230)
com.mongodb.util.JSONParser.parse(JSON.java:155)
com.mongodb.util.JSON.parse(JSON.java:92)
com.mongodb.util.JSON.parse(JSON.java:73)
org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:71)
FYI, the parsing issue will be fixed in the next release of OGM.
Note that it will support NumberLong but not NumberInt as NumberInt is not supported by the MongoDB Java Driver: https://jira.mongodb.org/browse/JAVA-2185 .
The use of functions like NumberInt is not supported at the moment.
I've created an issue for it: https://hibernate.atlassian.net/browse/OGM-1027

MONGO DB Alias name usage in find query not working

i am using the below command to find records in contacts collection's
db.contacts.find({"field1":"1"})
the problem is i want to use alias name for field1 as SL.NO
i tried this way and its not working...
db.contacts.find({"field :n, as: :SL.NO" : "1"}
can anyne help me in this issue?
BTW I am currently using mongo 1.6.5
Some drivers like Mongoose support that feature, but there is no such feature in MongoDB itself or the Mongo shell.

How to clear/drop/empty a MongoDb collection with Casbah

I started using MongoDb in Scala via Casbah but cannot find on the Casbah documentation / google the way to drop the content of a collection.
The MongoDd doc says the MongoDb shell command to do so is
db.things.remove({});
But how can we achieve the same via Casbah?
Thanks in advance,
Olivier
Casbah's equivalent to the shells {} empty document operator is `MongoDBObject.empty
This should work -
db.things.remove(MongoDBObject.empty)