[英]Find and delete last entered entry from MongoDB collection
我添加了一个需要从 mongodb 集合中删除的元素。 这是我输入的最后一个条目。 我一直在关注这个链接: How to delete the last item of a collection in mongodb 。 但是,我找不到 ID,也无法从 Cursor (?) 中提取此值。 这是我到目前为止所做的:
const db = client.db(databaseName);
let x = db
.collection("ford_twitter")
.find()
.sort({_id:-1});
终端日志是:
如果它是最后一个条目,首先使用find
检查它是否存在。 找到后,您可以将其删除。
我会给你一个完整的例子来说明如何实现它:
async function find(){
try{
const call = await MongoClient.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
})
result = await call.db("todos")
.collection("quotes")
.find({})
.sort("_id", -1).limit(1)
.toArray()
console.log(result)
} catch(e){console.log(e)}
}
find()
在 MongoDB 中,有几个新对象,例如 Cursor。 要获得标准对象,您需要阅读如何使用 Cursor:它具有属性和方法...
toArray()
将给出该元素的数组,您可以对其进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.