MongoDB count() query returns outdated result in mongo shell - mongodb

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

Related

Executing commands from MongoDB Compass

Is it possible to execute a command such as db.eval("return new Date()") from MongoDB Compass?
Right now I see only collection query commands like filter, project, sort, etc.
Mongo compass provides only filter option (Query Bar) to do queries on specific collection :(
You can read how it works - https://docs.mongodb.com/compass/master/schema/#query-bar
Replying to old thread: Just want to mention that Mongo Compass v1.22 and newer has Embedded Shell which can do shell commands https://docs.mongodb.com/compass/current/embedded-shell/

How to get Collections name from Cosmos DB using Mongo API

I am trying to execute Mongo API to perform CRUD operation on Azure Cosmos-DB.
I am running the query on Azure Data explorer.
This is a query that I am executing {db.getCollectionNames()}
I am facing {"code":500,"body":"{\"message\":\"There was an error processing your request. Please try again in a few moments.\",\"httpStatusCode\":\"InternalServerError\",\"xMsServerRequestId\":null,\"stackTrace\":null}"}
Can you please suggest the changes if I am doing something wrong here.
the Mongo Query area is not the same as a native MongoDB shell. That is, the only thing you can do within the query window is execute find() queries, and you only specify the filtering (between the {}). For example:
There's also the ability to open a mongo shell via the browser, where you can run queries, in the more traditional format for mongo:
With the browser-based shell, you can also do updates (e.g. db.families.update()) and deletes (db.families.remove()). But it doesn't support commands such as db.getCollectionNames().

No WriteResult returned in Mongo shell when inserting/updating a row in MongoDB

I am able to successfully insert a document using the db.collection.insert() statement in my mongo shell However, the mongo shell does not return the WriteResult object.
Do I need to set this as a configuration? I watched few tutorial but it never mentioned of any.

Run mongo index command from terminal, solutions for a very large index

I'm trying to enable full text search on my mongo instance, but the collection I'm indexing is quite large. Normally, I'd open up the mongo console and type:
db.articles.ensureIndex({"text":"text"})
However, I've waited an hour and would like to run this overnight, so I'm seeking the ability to run something like a nohup command.
I am aware I can do some things like:
mongo dbname --eval "db.articles.ensureIndex({'text':'text'})"
But even rephrasing this as:
nohup mongo dbname --eval "db.articles.ensureIndex({'text':'text'})" &
This is still totally blocking my mongo usage. That is,
$ mongo
> show dbs
Times out while the other job is running in the background---completely unusable while indexing. Is there any way to run this more effectively as a background process, or limit the index command to only so many threads or something?
UPDATE
Actually, it's worse than that. Running this index command seems to have my mongo blocked and hobbled entirely. Very, very big index, I assume.
You should use the background option.
db.collection.ensureIndex({ text: 'text', background: true })
From mongodb's documentation:
Builds the index in the background so that building an index does not
block other database activities.
More information here http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/

When using a separate MongoDB with meteor, meteor reset stopped working

I have my MONGO_URL set to mongodb://localhost:27017/meteor and have the MongoDB run as a service.
When running my project it seems OK to store data to the separate MongoDB until I tried to run meteor reset.
My assumption is it tried to remove its default database. The error complained that myproject.meteor\local is not empty and pointed to fs.js:456 which goes to files.js:256 (rm_recursive) and so on.
any idea what and how I can fix this?
$ meteor reset only resets the bundled MongoDB. It won't reset an external Mongo database.
(That's something we should explain better in the documentation.)
In your case, try connecting to the Mongo database directly (with the mongo command line shell) and running > db.dropDatabase()