Log to console In Mongodb

John Smith picture John Smith · Apr 30, 2015 · Viewed 18.4k times · Source

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

 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({});

Opened the console with:

  mongo --shell

And tried to run the command:

  > load('mongo.js')
  true

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

Answer

codeofnode picture codeofnode · Apr 30, 2015

In the shell script, instead of console.log you can use

print() // for plain texts,

or printjson() // for json

usage :

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