如何使用node-mongodb-native驱动程序删除文档?
操作返回说deleted: 0
const res = await ctx.db.collection(this.col).removeOne({ _id: ctx.params.id });
不知道我在这里做错了什么。 { _id: <id> }
GET请求似乎正常工作。
ctx.params.id
被定义,并且与数据库中的ObjectId相同。
根据这个文档,你可以做collection.removeOne()
(参见示例2) https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#remove
// Remove all the document collection.removeOne({a:1}, {w:1}, function(err, r) { test.equal(null, err); test.equal(1, r.result.n); db.close(); });
尝试将ctx.params.id
为ObjectId
,这是mongodb在内部存储标识符的方式。
import { ObjectId } from 'mongodb' id = ObjectId(ctx.params.id)