MongoDB: $and 运算符无法使用 Node.JS 驱动程序进行批量更新

MongoDB: $and operator not working in Bulk updates using the Node.JS driver

我正在尝试使用来自 Node.js 的 MongoDB 3.0.4 中的 Bulk update API。奇怪的是,如果使用 $and$ne 等运算符,我会收到以下错误。简单的查询虽然有效。

WriteError({"code":9,"index":0,"errmsg":"Unknown modifier: $and","op":{"q":{"$and":[{"id":49689},{"status":{"$ne":4}}]},"u":{"$and":[{"id":49689},{"status":{"$ne":4}}]},"multi":true,"upsert":false}})

下面是 CoffeeScript 中的一些测试代码:

  bulk = db.mongo.collection('route-segments').initializeUnorderedBulkOp()
  filter = $and: [
      { id: 49689 }
      { status: $ne: 4 }
  ]
  # filter = id: segStatus.id # <-- this works
  bulk.find(filter).updateOne(filter, $set: status: 4)
  bulk.execute (err, result) ->
    console.log "err = #{err}"

我在 Mongo shell 中尝试了相同的命令并且成功了:

> bulk=c.initializeUnorderedBulkOp()
{ "nInsertOps" : 0, "nUpdateOps" : 0, "nRemoveOps" : 0, "nBatches" : 0 }
> bulk.find({ $and: [{id: 49689}, {status: {$ne: 4}}]}).updateOne( {$set: {status: 4}})
> bulk.execute()
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 0,
    "nUpserted" : 0,
    "nMatched" : 1,
    "nModified" : 1,
    "nRemoved" : 0,
    "upserted" : [ ]
})

Mongo数据库版本:3.0.4
MongoDB Node.js 驱动程序:2.0.35

你不应该在 updateOne 方法中使用 filter

http://docs.mongodb.org/manual/reference/operator/update/#id1