Executing commands from MongoDB Compass - mongodb

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/

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()

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().

how to show my users collection with mongodb

I'm sorry for this (peraphs) stupid question ... I install meteor and mongodb in my windows computer and i start to write some apps. I don't understand how to use mongo for shoving my db app ... i open one shell in my app dir and launch mongod, in one more shell in the same folder i start mongo.
show dbs
local
use local
switched to db local
show collections
startup_log
system.indexes
Where are my collections? Where is users collection?
When your app is running use this command on a separate command line mongo 127.0.0.1:3001
Meteor keeps the collections in this server. After you run mongo on this server, by writing use meteor you can use db specific to your running app. And then you can display your collections with db.getCollectionNames()
Meteor uses a library called Minimongo that's why it doesn't display if you run show dbs on your mongo shell.
By default it points to port 3001 hence if you are using Robomongo you can just make the set up to watch that port.
To display all your MongoDB collections using the shell, you may check this answer:
How to list all collections in the mongo shell?
You may also use a MongoDB GUI Tool such Robomongo

How can I query an FS collection in Meteor from the command line?

It is very useful to run meteor mongo and query collections from the command line, for debugging purposes, etc.
Recently, I have added the collectionFS package to enable image storage in the database. However, I am unable to query the database from the command line.
db.fs.collection_name.find() is not doing the trick, and I can't seem to find the correct command anywhere.
Go to the Meteor Mongo console: meteor mongo
See all the collections that are available: show collections
Look for the one that has cfs.collection_name.files
Choose the one that has your collection name. For example, I'm using collectionFS with gridFS for images. When I type show collections, I see cfs_gridfs.images.files so I just do: db.cfs_gridfs.images.files.find() to see those files.
Hope that helps.
If you find it difficult to use the command line or terminal, you have a UI for MongoDB called Robomongo which is easy to install and use. I use Meteor with its default port number and then in Robomongo it is used as 3001.
And the query to view collection here is same as db.collection_name.find().