如何删除 mongodb 中的数组项?
how do i delete array item in mongodb?
我想根据 mongo db 中项目架构中的注释数组中的“noteID”的值删除一个元素。
项目架构
endDate: {
type: String,
required: true,
default: Date.now().toString()
},
notes: {
type: [NoteSchema],
required: false
},
users: [{
type: String,
required: false
}]
笔记架构
const NoteSchema = mongoose.Schema({
noteID: {
type: String,
require: true
},
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
endDate: {
type: Date,
default: Date.now()
},
priority: {
type: String,
default: "low"
}
});
使用 express
返回 json
"endDate": "11.11.2021",
"notes": [
{
"endDate": "2022-02-05T08:02:18.166Z",
"priority": "low",
"_id": "61fe2f423667ad054b62f84f",
"noteID": "cb978ec0-7229-4253-9e36-eaaf85b72ae3",
"title": "test note title",
"description": "test note desc"
},
{
"endDate": "2022-02-05T08:02:18.166Z",
"priority": "medium",
"_id": "61fe2f593667ad054b62f851",
"noteID": "760fda9f-e453-4e9c-bc9f-161f38fd29c0",
"title": "test note title 2",
"description": "test note desc 2"
}
],
"users": [],
我想使用 noteID 从这个笔记目录中删除一个项目。
基本上,您可以像 mongoose 中那样删除架构项。
YourColletionName.findOneAndDelete({ noteID: "cb978ec0-7229-4253-9e36-eaaf85b72ae3" },
function (err, data) {
if (err){
console.log(err)
}
else{
console.log("Removed: ", data);
}
});
如果你想删除页面上的noteID link,你可以使用req.params.noteID而不是cb978ec0-7229- 4253-9e36-eaaf85b72ae3.
资源:
使用 update with $pull 通过 noteID 从数组中删除元素:
db.collection.update({},
{
$pull: {
notes: {
noteID: "cb978ec0-7229-4253-9e36-eaaf85b72ae3"
}
}
})
解释:
上述查询将从对象数组 notes[] 中删除节点 ID 为“cb978ec0-7229-4253-9e36-eaaf85b72ae3”的所有对象。该操作适用于集合中第一个找到的文档。如果您需要应用于集合中的所有文档,则需要添加 { multi:true } 选项
我想根据 mongo db 中项目架构中的注释数组中的“noteID”的值删除一个元素。
项目架构
endDate: {
type: String,
required: true,
default: Date.now().toString()
},
notes: {
type: [NoteSchema],
required: false
},
users: [{
type: String,
required: false
}]
笔记架构
const NoteSchema = mongoose.Schema({
noteID: {
type: String,
require: true
},
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
endDate: {
type: Date,
default: Date.now()
},
priority: {
type: String,
default: "low"
}
});
使用 express
返回 json"endDate": "11.11.2021",
"notes": [
{
"endDate": "2022-02-05T08:02:18.166Z",
"priority": "low",
"_id": "61fe2f423667ad054b62f84f",
"noteID": "cb978ec0-7229-4253-9e36-eaaf85b72ae3",
"title": "test note title",
"description": "test note desc"
},
{
"endDate": "2022-02-05T08:02:18.166Z",
"priority": "medium",
"_id": "61fe2f593667ad054b62f851",
"noteID": "760fda9f-e453-4e9c-bc9f-161f38fd29c0",
"title": "test note title 2",
"description": "test note desc 2"
}
],
"users": [],
我想使用 noteID 从这个笔记目录中删除一个项目。
基本上,您可以像 mongoose 中那样删除架构项。
YourColletionName.findOneAndDelete({ noteID: "cb978ec0-7229-4253-9e36-eaaf85b72ae3" },
function (err, data) {
if (err){
console.log(err)
}
else{
console.log("Removed: ", data);
}
});
如果你想删除页面上的noteID link,你可以使用req.params.noteID而不是cb978ec0-7229- 4253-9e36-eaaf85b72ae3.
资源:
使用 update with $pull 通过 noteID 从数组中删除元素:
db.collection.update({},
{
$pull: {
notes: {
noteID: "cb978ec0-7229-4253-9e36-eaaf85b72ae3"
}
}
})
解释:
上述查询将从对象数组 notes[] 中删除节点 ID 为“cb978ec0-7229-4253-9e36-eaaf85b72ae3”的所有对象。该操作适用于集合中第一个找到的文档。如果您需要应用于集合中的所有文档,则需要添加 { multi:true } 选项