Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log to console In Mongodb

Tags:

mongodb

robo3t

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

like image 640
John Smith Avatar asked Apr 30 '15 19:04

John Smith


People also ask

How do I access MongoDB console?

To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.

How do I access MongoDB logs?

MongoDB logs can be found in the MongoDB log files at /var/log/mongodb/mongodb. log. If you can't find the log files from this location, you can check the mongodb. conf.

How do I view MongoDB data in terminal?

In MongoDB, you can use the show dbs command to list all databases on a MongoDB server. This will show you the database name, as well as the size of the database in gigabytes. You can select any database using the use statement and work on it.


2 Answers

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

like image 82
codeofnode Avatar answered Sep 29 '22 12:09

codeofnode


When 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 output.

like image 20
maskillbt Avatar answered Sep 29 '22 11:09

maskillbt