更新插入不适用于 updateOnebulkWrite v3.4

Jon*_*sen 2 mongodb node.js

我正在尝试批量写入一些更新,除了更新插入之外的所有内容都正常。
我的代码完美地更新了所有项目,并且没有给出任何错误。
这里的问题是批量插入updateOne没有更新插入。

(这是我的代码的未经测试和缩短的示例,因此您可能会发现一些语法错误。希望您明白。)

async function insert(items) {
    const ops = items.map(item => ({
        updateOne: {
            filter: {
                id: item.id,
                country: item.country,
            },
            update: { $set: item },
            upsert: true
        }
    }));

    return await db.collection.bulkWrite(ops);
}

insert([{
    id: '123',
    country: 'uk',
    email: 'test@test.com'
}]);
Run Code Online (Sandbox Code Playgroud)

上面的代码片段不会更新插入缺失的项目,但会正确更新其他所有内容。

我正在使用node version 6.6.0andmongodb version v3.4.10并根据 3.4 的 mongodb 文档(https://docs.mongodb.com/v3.4/reference/method/db.collection.bulkWrite/),bulkwrite3.2 版本中的新增功能。

此片段来自 MongoDB 3.4 文档bulkWrite

db.collection.bulkWrite( [
   { updateOne :
      {
         "filter" : <document>,
         "update" : <document>,
         "upsert" : <boolean>
      }
   }
] )
Run Code Online (Sandbox Code Playgroud)

这是同一文档中的示例。

{
    updateOne: {
        "filter": { "char": "Eldon" },
        "update": { $set: { "status": "Critical Injury" } }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试直接在 mongo 终端界面中使用完全相同的数据进行正常更新,并正确更新插入项目。

db.collection.update({ id: '123', country: 'uk' }, { $set: { id: '123', country: 'uk', email: 'test@test.com' } }, { upsert: true });
Run Code Online (Sandbox Code Playgroud)

Ket*_*vya 6

const ops = items.map(item => 
           ({ updateOne: { 
              filter: { id: item.id, country: item.country}, 
              update: { $set: {item} }, upsert: true } 
           }));
Run Code Online (Sandbox Code Playgroud)

$set by 语法期望{ id: item.id, Country: item.country}而不是仅 传递id: item.id, Country: item.country 。