MongoDB:$和操作符不能使用Node.JS驱动程序进行批量更新
我正尝试在Node.js中使用MongoDB 3.0.4中的批量更新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中的一些testing代码:
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" : [ ] })
MongoDB版本:3.0.4
MongoDB Node.js驱动程序:2.0.35
你不应该在updateOne方法中使用filter
。
http://docs.mongodb.org/manual/reference/operator/update/#id1