使用ObjectId(nodejs
driver doc)
当您具有表示BSON ObjectId的字符串(例如,从Web请求接收)时,则需要将其转换为ObjectId实例:
const {ObjectId} = require('mongodb'); // or ObjectID // or var ObjectId = require('mongodb').ObjectId if node version < 6const updateStuff = (id, doc) => { // `ObjectId` can throw https://github.com/mongodb/js-bson/blob/0.5/lib/bson/objectid.js#L22-L51, it's better anyway to sanitize the string first if (!ObjectId.isValid(s)) { return Promise.reject(new TypeError(`Invalid id: ${id}`)); } return collection.findoneAndUpdate( {_id: ObjectId(id)}, {$set: doc}, {returnOriginal: false} );};


