登录到Mongodb中的控制台

Joh*_*ith 9 mongodb robo3t

当我在robomongo中运行此命令时,我得到一个包含不同行的输出:

 db.getCollection('houses').find({})
Run Code Online (Sandbox Code Playgroud)

现在我尝试在mongo shell中运行相同的命令:

我写了一个脚本mongo.js:

  conn = new Mongo();
  db = conn.getDB("development");

  db.getCollection('houses').find({});
Run Code Online (Sandbox Code Playgroud)

打开控制台:

  mongo --shell
Run Code Online (Sandbox Code Playgroud)

并尝试运行命令:

  > load('mongo.js')
  true
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我只得到true输出.我想看看查询输出!我错了什么?谢谢

cod*_*ode 24

在shell脚本中,而不是console.log你可以使用

print() //用于纯文本,

printjson()//为json

用法:

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


小智 5

使用时

printjson(db.getCollection('houses').find({})); 
Run Code Online (Sandbox Code Playgroud)

我从 find 对象获得输出。

{
"_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({...})")
 ...........
}
Run Code Online (Sandbox Code Playgroud)

如果你使用

db.getCollection('houses').find({}).forEach(printjson)
Run Code Online (Sandbox Code Playgroud)

我得到了想要的输出。