【问题标题】:Saving the result of find() to another collection将 find() 的结果保存到另一个集合
【发布时间】:2018-12-11 08:13:24
【问题描述】:

我想将 find() 命令的结果保存到新集合中(基于对类似问题 Saving the result of a MongoDB query 的回答)。

> db.collection2.insert(db.collection1.find({"person.name": "Carl"}))

那我想看看这是否成功

> db.righthand.find()
[Object]

我不确定它为什么会输出 [Object],因为我认为它会插入 db.collection1.find() 的结果。

【问题讨论】:

    标签: mongodb find


    【解决方案1】:

    您在控制台中获得了[Object],因为您插入了由 find() 方法返回的cursor

    您真正需要的是在光标上使用 toArray() 方法,因为它会返回一个包含其中所有文档的数组。该方法完全迭代游标,将所有文档加载到RAM中并耗尽游标。

    所以你的插入操作看起来像

    > db.collection2.insert(db.collection1.find({"person.name": "Carl"}).toArray())
    

    并将集合查询为

    > db.collection2.find().pretty()
    

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多