Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo dbname --eval 'db.collection.find()' does not work

Tags:

Why does this work:

# mongo dbname
MongoDB shell version: 1.8.3
connecting to: nextmuni_staging
> db.collection.find()
{ "foo" : "bar" }
> bye

While this does not work:

# mongo localhost/dbname --eval 'db.collection.find()'
MongoDB shell version: 1.8.3
connecting to: localhost/dbname
DBQuery: dbname.collection -> undefined

It should be exactly the same, no?

Thanks!

like image 541
charlax Avatar asked Sep 08 '11 18:09

charlax


People also ask

How do I find a particular collection in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.

What does command FIND () do in MongoDB?

To find documents that match a set of selection criteria, call find() with the <criteria> parameter. MongoDB provides various query operators to specify the criteria. For a list of the query operators, see Query Selectors.

How do I show all files in a collection in MongoDB?

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database.

How do I select a database in Mongosh?

Open a new database connection. Open a connection to a new server using new Mongo() . Use getDB() method of the connection to select a database.


2 Answers

The return val of db.collection.find() is a cursor type. Executing this command from within the shell will create a cursor and show you the first page of data. You can start going through the rest by repeating the 'it' command.

I think the scope of variables used during the execution of an eval'd script is only for the lifetime of the script (data can be persisted into collections of course) so once the script terminates those cursor variables no longer exist and so you would be able to send another eval script to page the data. So the behaviour you get during a shell session wouldn't really work from an eval script.

To get close to the behaviour you could run something like this:

mongo dbname --eval "db.collection.find().forEach(printjson)"

That shows you that the command does execute and produce a cursor which you can then iterate over sending the output to stdout.

Edit: I think the point I was trying to make was that the command you are issuing is working its just the output is not what you expect.

like image 187
JB. Avatar answered Oct 26 '22 04:10

JB.


The printjson functions covers a lot of ground when scripting with mongo --eval '...'. Rather than chaining .forEach you can simply wrap your call.

$ mongo --eval 'db.stats_data.stats()' db_name
MongoDB shell version: 2.4.14
connecting to: db_name
[object Object]

$ mongo --eval 'db.stats_data.stats().forEach(printjson)' db_name
MongoDB shell version: 2.4.14
connecting to: db_name
Tue Jan 10 15:32:11.961 TypeError: Object [object Object] has no method 'forEach'

$ mongo --eval 'printjson(db.stats_data.stats())' db_name
MongoDB shell version: 2.4.14
connecting to: db_name
{
    "ns" : "db_name.stats_data",
    "count" : 5516290,
    "size" : 789938800,
    "avgObjSize" : 143.20110073980882,
    "storageSize" : 1164914688,
    "numExtents" : 18,
    "nindexes" : 3,
    "lastExtentSize" : 307515392,
    "paddingFactor" : 1.0000000000000457,
    "systemFlags" : 1,
    "userFlags" : 0,
    "totalIndexSize" : 1441559616,
    "indexSizes" : {
        "_id_" : 185292688,
        "owner_id_key_idx" : 427678384,
        "onwer_metric_key_idx" : 828588544
    },
    "ok" : 1
}
like image 23
Bruno Bronosky Avatar answered Oct 26 '22 03:10

Bruno Bronosky