Javascript 如何删除子文档mongoosejs中的元素
如何删除该特定用户的“book”“id\u book=8522”中带有“id\u extra=8523”的“extras”?我目前使用mongoosejs,但没有成功,我尝试了很多东西,但无论我尝试了什么,都只能工作到嵌套文档的第一级Javascript 如何删除子文档mongoosejs中的元素,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,如何删除该特定用户的“book”“id\u book=8522”中带有“id\u extra=8523”的“extras”?我目前使用mongoosejs,但没有成功,我尝试了很多东西,但无论我尝试了什么,都只能工作到嵌套文档的第一级 { "_id":{ "$oid":"5e32fce08919b53ad66cf694" }, "user_id":{ "$numberInt":"258787" }, "username":"daaaa",
{
"_id":{
"$oid":"5e32fce08919b53ad66cf694"
},
"user_id":{
"$numberInt":"258787"
},
"username":"daaaa",
"firstname":"davide",
"api_key":"7b031c21edf1237c554867622ad1154f",
"books":[
{
"_id":{
"$oid":"5e356323a0ef6319ebb60162"
},
"id_book":{
"$numberInt":"8522"
},
"blob_annotation":null,
"extra_id":{
"$numberInt":"995176"
},
"extras":[
{
"_id":{
"$oid":"5e356324a0ef6319ebb60163"
},
"id_extra":{
"$numberInt":"8523"
},
"type":"gallery_audio",
"label":"Inverno a Boscodirovo"
},
{
"_id":{
"$oid":"5e356324a0ef6319ebb60164"
},
"id_extra":{
"$numberInt":"8524"
},
"type":"gallery_audio",
"label":"Storia di Primavera"
}
],
"raccolte":[
]
}
],
}
您可以执行以下操作:使用“book”“id\u book=8522”中的“id\u extra=8523”过滤
“extras”,然后在MongoDB中再次保存文档
db.collection.aggregate([
{
$match: {
"books.id_book": 8522
}
},
{
$addFields: {
"books": {
$reduce: {
input: "$books",
initialValue: [],
in: {
$concatArrays: [
"$$value",
[
{
_id: "$$this._id",
id_book: "$$this.id_book",
blob_annotation: "$$this.blob_annotation",
extra_id: "$$this.extra_id",
raccolte: "$$this.raccolte",
extras: {
$filter: {
input: "$$this.extras",
cond: {
$ne: [
"$$this.id_extra",
8523
]
}
}
}
}
]
]
}
}
}
}
}
])
您可以执行以下操作:使用“book”“id\u book=8522”的“id\u extra=8523”过滤附加文件,然后在MongoDB中再次保存文档
db.collection.aggregate([
{
$match: {
"books.id_book": 8522
}
},
{
$addFields: {
"books": {
$reduce: {
input: "$books",
initialValue: [],
in: {
$concatArrays: [
"$$value",
[
{
_id: "$$this._id",
id_book: "$$this.id_book",
blob_annotation: "$$this.blob_annotation",
extra_id: "$$this.extra_id",
raccolte: "$$this.raccolte",
extras: {
$filter: {
input: "$$this.extras",
cond: {
$ne: [
"$$this.id_extra",
8523
]
}
}
}
}
]
]
}
}
}
}
}
])
尝试与以下内容结合使用:
请尝试结合使用:
有没有拉的地方?@Davide看一看maforanswer没有拉的地方?@Davide看一看maforanswer你试过什么?这就是帮助的来源你尝试过什么?这就是帮助的来源