[英]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.