我试图用json格式从mongo数据库中提取记录,以便在js下运行:

conatin of get_DRMPhysicalresources_Data.js
    cursor = db.physicalresources.find().limit(10)
    while ( cursor.hasNext() ){
       print( JSON.stringify(cursor.next()) );
    }

the command i run to get the records : 
    mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json

我能够在DRM物理资源中以josn formate的形式得到结果.json,现在我想使用use命令切换到其他数据库,我try 添加use db,如下所示:

 conatin of get_DRMPhysicalresources_Data.js
    use db2_test
    cursor = db.physicalresources.find().limit(10)
    while ( cursor.hasNext() ){
       print( JSON.stringify(cursor.next()) );
    }
    the command i run to get the records : 
    mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json

但我发现以下错误:

MongoDB shell version v4.2.3
connecting to: "some data base info"
Implicit session: session { "id" : UUID("8c85c6af-ebed-416d-9ab8-d6739a4230cb") }
MongoDB server version: 4.4.11
WARNING: shell and server versions do not match
2022-04-11T13:39:30.121+0300 E  QUERY    [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell eval):1:1
2022-04-11T13:39:30.122+0300 E  QUERY    [js] Error: error loading js file: get_DRMPhysicalresources_Data.js :
@(shell eval):1:1
2022-04-11T13:39:30.122+0300 E  -        [main] exiting with code -4

有没有办法在不中断db2_测试的情况下添加use db2_测试?

推荐答案

你可以试试这个:

 db = new Mongo().getDB("myOtherDatabase");
 

 db = db.getSiblingDB('myOtherDatabase')

(这与"使用数据库"助手完全相同)

docs

Mongodb相关问答推荐

从MongoDB中的一个非空字段获取值

MongoDB 对特定搜索查询的响应时间较长

如何匹配 MongoDB 中同一文档中两个字段的比较?

如何对 MongoDB setWindowFields 中当前文档以外的文档进行操作

Mongodb,在一个查询中用正则表达式更新部分字符串

MongoDB聚合:如何将查找结果放入嵌套数组中?

找到一个用户,然后使用 MongoDB 根据他们的总分获得他们的排名

使用golang的MongoDB错误无法访问服务器

如何将以下聚合查询转换为 bson 并在 golang 中执行

嵌套数组 $unwind 和 $group 在 mongoDB 中重新组合在一起

如何在 Mongo 聚合管道中获取限制之前的计数

使用 Flask-pymongo 扩展通过 _id 在 MongoDB 中搜索文档

根据 Month 删除 mongodb 中的旧记录

Mongoose 是否真的验证了对象 ID 的存在?

Node + Mongoose:获取最后插入的 ID?

C# mongodb driver 2.0 - 如何在批量操作中更新插入?

majority和 linearizable的区别

是否有支持 MongoDB 和 Devise 的 Rails 管理界面?

使用 mongodb 在数组中查找子文档

使用自定义 _id 值时 mongodb 中的 Upserts