Login to console in Mongodb

When I run this command in robomongo, I get output with different lines:

db.getCollection('houses').find({}) 

Now I tried to run the same command in the mongo shell:

I wrote a script mongo.js :

  conn = new Mongo(); db = conn.getDB("development"); db.getCollection('houses').find({}); 

Open the console with

  mongo --shell 

And tried to run the command:

  > load('mongo.js') true 

I do not understand why I only get true as output. I want to see the request exit! What am I wrong? Thanks

+6
source share
2 answers

In a shell script instead of console.log you can use

print() // for simple texts,

or printjson() // for json

using:

printjson(db.getCollection('houses').find({}));

+15
source

Using

 printjson(db.getCollection('houses').find({})); 

I get the output from the find object.

 { "_mongo" : connection to ***.***.***.***, "_db" : *****, "_collection" : ***.houses, "_ns" : "*****.houses", "_query" : { }, "_fields" : null, "_limit" : 0, "_skip" : 0, "_batchSize" : 0, "_options" : 4, "_cursor" : null, "_numReturned" : 0, "_special" : false, "help" : function () { print("find(<predicate>, <projection>) modifiers") print("\t.sort({...})") ........... } 

if you use

 db.getCollection('houses').find({}).forEach(printjson) 

I get the desired result.

+1
source

Source: https://habr.com/ru/post/986324/


All Articles