【问题标题】:running js file for mongo shell运行 mongo shell 的 js 文件
【发布时间】:2014-03-25 22:53:03
【问题描述】:

我写了一个小的 js 文件,它应该列出当前 MongoDB 实例中的所有 DB,并在其中一个中找到一个文档(db = 数据库,集合 = col)

test.js

print(db.getMongo().getDBs())
db.getSiblingDB('database')
out = db.col.findOne()
print(out)

但是,输出是

# mongo test.js

MongoDB shell version: 2.4.9
connecting to: test
[object Object]
null

我错过了什么?

干杯,M

【问题讨论】:

    标签: javascript mongodb mongo-shell


    【解决方案1】:

    一些事情:

    printjson(db.getMongo().getDBs());
    var db = db.getSiblingDB('database');
    var out = db.col.findOne();
    printjson(out);
    

    除非您使用printjson,否则您可能看不到所需的输出。还有.getSiblingDB()返回数据库对象,所以你需要“设置”变量。

    最后,“总是”使用var 来避免REPL“评估”每一行。这也是您可能不想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多