从输入数组的每个值更新文档

时间:2018-06-12 23:21:35

标签: mongodb mongo-shell

我有一个带有两个密钥的JSON:id和name。我需要一种方法在我的集合(querys.project.name)上插入该ID,当它的名称和JSON上的匹配时。

我的JSON示例:

var projectsMysql = [
{
    "id" : 1,
    "name" : "Something"
},
{
    "id" : 5,
    "name" : "Something else"
},
{
    "id" : 50,
    "name" : "Some name"
}]

在我的收藏中大约有60个这样的文件:

  {
  "_id" : ObjectId("58e42bf30a34d641be6c25c2"),
  "folio" : "R-666-69",
  "alias_purchase" : "Deal",
  "project" : {
      "description" : "",
      "name" : "Something else"
      // "projectsMysql_id" : 5 THIS SHOULD BE PLACED HERE
  },
}

我为Node.js编写了一个函数,但现在我需要在mongo shell上直接运行它,我读到我需要使用cursor.forEach()方法,但是我找不到办法来做到这一点,这个是我的功能:

projectsMysql
.forEach(function(project){
var query = {
  name: project.name
}
db.getCollection('requests')
  .find(query)
  .exec(function(err, response){
    if (err) {
      return 
    }
    if (response) {
      db.getCollection('requests')
        .update({id: response.id}, {$set: {
          'project.projectsMysql_id': project.id
           console.log("element inserted"); 
        }})
        .exec(function(err){
        });
    }
  });
});

你能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

您想要的是bulkWrite(),而不是为update()数组中的每个文档实际执行projectsMysql,而是实际构建一个"单个"声明由"多个"发送到服务器并更新相应文档的更新:

db.getCollection('requests').bulkWrite(
  projectsMysql.map(({ id, name }) => 
    ({ "updateOne": {
      "filter": { "project.name": name },
      "update": { "$set": { "project.id": id } }
    }})
  )
)

如果您希望"多个文件"要匹配条件,只需切换到updateMany

db.getCollection('requests').bulkWrite(
  projectsMysql.map(({ id, name }) => 
    ({ "updateMany": {
      "filter": { "project.name": name },
      "update": { "$set": { "project.id": id } }
    }})
  )
)

你的数组已经在内存中了,所以没有任何意义可以做任何其他类型的#34;迭代"您只需将.map()属性$set放到updateOne语句(或updateMany)上,然后在一个语句中将它们全部发出。更新只会在有"匹配的情况下实际处理。确实存在要实际更新的地方,因为使用What is PECS (Producer Extends Consumer Super)?将保留现有值。

要证明,考虑这些文件:

{
  "folio" : "R-666-69",
  "alias_purchase" : "Deal",
  "project" : {
      "description" : "",
      "name" : "Something else"
  }
},
{
  "folio" : "R-666-67",
  "alias_purchase" : "Deal",
  "project" : {
      "description" : "",
      "name" : "Some name"
  }
},
{
  "folio" : "R-666-68",
  "alias_purchase" : "Deal",
  "project" : {
      "description" : "",
      "name" : "Some name"
  }
}
{
  "folio" : "R-666-64",
  "alias_purchase" : "Deal",
  "project" : {
      "description" : "",
      "name" : "Something"
  }
},
{
  "folio" : "R-666-65",
  "alias_purchase" : "Deal",
  "project" : {
      "description" : "",
      "name" : "Different"
  }
}

以下更新:

var projectsMysql = [
{
    "id" : 1,
    "name" : "Something"
},
{
    "id" : 5,
    "name" : "Something else"
},
{
    "id" : 50,
    "name" : "Some name"
}]

db.getCollection('requests').bulkWrite(
  projectsMysql.map(({ id, name }) => 
    ({ "updateMany": {
      "filter": { "project.name": name },
      "update": { "$set": { "project.id": id } }
    }})
  )
)

返回响应:

{
        "acknowledged" : true,
        "deletedCount" : 0,
        "insertedCount" : 0,
        "matchedCount" : 4,
        "upsertedCount" : 0,
        "insertedIds" : {

        },
        "upsertedIds" : {

        }
}

并相应地改变匹配的文件:

{
        "_id" : ObjectId("5b206a48f7fa0c655d90157a"),
        "folio" : "R-666-69",
        "alias_purchase" : "Deal",
        "project" : {
                "description" : "",
                "name" : "Something else",
                "id" : 5
        }
}
{
        "_id" : ObjectId("5b206a48f7fa0c655d90157b"),
        "folio" : "R-666-67",
        "alias_purchase" : "Deal",
        "project" : {
                "description" : "",
                "name" : "Some name",
                "id" : 50
        }
}
{
        "_id" : ObjectId("5b206a48f7fa0c655d90157c"),
        "folio" : "R-666-68",
        "alias_purchase" : "Deal",
        "project" : {
                "description" : "",
                "name" : "Some name",
                "id" : 50
        }
}
{
        "_id" : ObjectId("5b206a48f7fa0c655d90157d"),
        "folio" : "R-666-64",
        "alias_purchase" : "Deal",
        "project" : {
                "description" : "",
                "name" : "Something",
                "id" : 1
        }
}
{
        "_id" : ObjectId("5b206a48f7fa0c655d90157e"),
        "folio" : "R-666-65",
        "alias_purchase" : "Deal",
        "project" : {
                "description" : "",
                "name" : "Different"
        }
}