node.js - 如何使用 node-mongodb-native 驱动程序删除文档?

标签 node.js mongodb

该操作返回 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)

关于node.js - 如何使用 node-mongodb-native 驱动程序删除文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051054/

相关文章:

mongodb - MongoChef聚合: In one query find and show average score for max 3, 2和1 'project month'分组数据

deployment - 部署/构建/制作网络应用程序的基础知识是什么?

javascript - Node js 扩展继承对象(作为枚举)

node.js - Node.js 中 require() 参数确定文件扩展名的优先级

mongodb - 仅当该属性存在时,如何更新 mongo 中的嵌套对象?

node.js - 蒙戈数据库 : query by username

javascript - 如何让我的 Discord 机器人提及我提到的某人?

javascript - Node.js 中的异步递归

node.js - 即使字段从文档中删除,Mongoose 也会返回字段的默认值

python - 如何在 pandas 中连接数据框?