【问题标题】:Robo3T will only insert the first JSON object but not the second?Robo3T 只会插入第一个 JSON 对象而不会插入第二个?
【发布时间】:2020-04-07 02:42:00
【问题描述】:

我正在尝试将文档插入到 Robo3T 中的集合中。我有一个像这样的脚本:

db.permissions.insert({
    "_id" : "INS-SH-V",
    "name" : "View Sharing Functionality",
},
{
    "_id" : "INS-SH-U",
    "name" : "Manage Sharing Functionality",
});

但它只会插入“INS-SH-V”文档而不是第二个。我也尝试过将.insertMany{multi:true} 放在最后,但似乎没有任何效果。

当我执行insertMany 时,它会返回如下内容:

db.getCollection('permissions').insertMany({
    "_id" : "INS-SH-V",
    "name" : "View Sharing Functionality",
    "description" : "Access to view Email and Schedule windows in Insights",
},
{
    "_id" : "INS-SH-U",
    "name" : "Manage Sharing Functionality",
    "description" : "Access to send emails and schedule jobs in Insights",
})

当我使用{multi:true} 或基本上

db.getCollection('permissions').insert({
    "_id" : "INS-SH-V",
    "name" : "View Sharing Functionality",
},
{
    "_id" : "INS-SH-U",
    "name" : "Manage Sharing Functionality",
}, {multi:true})

它只会添加第一个文档“INS-SH-V”而不是另一个。

【问题讨论】:

    标签: mongodb robo3t


    【解决方案1】:

    请阅读documentation about db.collection.insertMany()

    insertMany() 方法的语法如下:

    db.collection.insertMany(
       [ <document 1> , <document 2>, ... ],
       {
          writeConcern: <document>,
          ordered: <boolean>
       }
    )
    

    insertMany 需要一个数组。

    db.getCollection('permissions').insert([{
        "_id" : "INS-SH-V",
        "name" : "View Sharing Functionality",
    },
    {
        "_id" : "INS-SH-U",
        "name" : "Manage Sharing Functionality",
    }])
    

    【讨论】:

      【解决方案2】:

      所以经过一番挖掘,我发现我必须将第一个参数作为数组传递。这是最终的结果:

      db.getCollection('permissions').insert([{
          "_id" : "INS-SH-V",
          "name" : "View Sharing Functionality",
      },
      {
          "_id" : "INS-SH-U",
          "name" : "Manage Sharing Functionality",
      }])
      

      【讨论】:

        猜你喜欢
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        • 2022-01-07
        • 2019-01-24
        • 1970-01-01
        • 2014-05-15
        • 1970-01-01
        相关资源
        最近更新 更多