gpt4 book ai didi

node.js - 如何在 MongoDB 中 $unset 整个对象而不是将其保留为空或 null

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:30 24 4
gpt4 key购买 nike

我有以下更新查询,它会删除对象的内容,但它会留下。我想删除整个对象{}

我怎样才能做到这一点?我也尝试过为 properties 指定 value 而不是 booleans,这给了我相同的结果

如果我执行此操作 { $unset: { "courses.1.teachers.1": 1 },则会返回 null 而不是 {}。所以它们都不是最佳的。

student.updateStudent = (fcallback) => {
var studentObjectId = global.mongoId("5a019896f89c24926108e2bf")
global.db.collection('students').updateOne({ "_id": studentObjectId }, { $unset: { "courses.1.teachers.1.firstName": 1, "courses.1.teachers.1.lastName": 1 } }, (err, result) => {
if (err) {
var jError = { "status": "error", "message": "ERROR -> user.js -> 001" }
console.log(jError)
return fcallback(true, jError)
}
var jOk = { "status": "ok", "message": "user.js -> deleted -> 000" }
console.log(jOk)
console.log(JSON.stringify(result))
return fcallback(false, jOk)
})
}

这是留下空对象的结果:

{
"_id" : ObjectId("5a019896f89c24926108e2bf"),
"firstName" : "Sarah",
"lastName" : "Jepsen",
"age" : 27,
"courses" : [
{
"courseName" : "Web-development",
"teachers" : [
{
"firstName" : "Santiago",
"lastName" : "Donoso"
}
]
},
{
"courseName" : "Databases",
"teachers" : [
{
"firstName" : "Dany",
"lastName" : "Kallas"
},
{} //see here
]
},
{
"teachers" : [
{
"firstName" : "Roxana",
"lastName" : "Stolniceanu"
}
]
}
]
}

我想要的是这样的:

{
"_id" : ObjectId("5a019896f89c24926108e2bf"),
"firstName" : "Sarah",
"lastName" : "Jepsen",
"age" : 27,
"courses" : [
{
"courseName" : "Web-development",
"teachers" : [
{
"firstName" : "Santiago",
"lastName" : "Donoso"
}
]
},
{
"courseName" : "Databases",
"teachers" : [
{
"firstName" : "Dany",
"lastName" : "Kallas"
}
]
},
{
"teachers" : [
{
"firstName" : "Roxana",
"lastName" : "Stolniceanu"
}
]
}
]
}

最佳答案

根据$unset docs ,这是所需的行为:

When used with $ to match an array element, $unset replaces the matching element with null rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.

所以您使用了错误的工具来完成这项工作。

正确的工具:

  1. $pop运算符:删除第一个或最后一个数组元素
  2. $pull运算符:根据条件删除项目

文档示例:

{
"_id" : ObjectId("5a019896f89c24926108e2bf"),
"firstName" : "Sarah",
"lastName" : "Jepsen",
"age" : 27,
"courses" : [
{
"courseName" : "Web-development",
"teachers" : [
{
"firstName" : "Santiago",
"lastName" : "Donoso"
}
]
},
{
"courseName" : "Databases",
"teachers" : [
{
"firstName" : "Dany",
"lastName" : "Kallas"
},
{
"firstName": "Code",
"lastName": "Dragon"
}
]
},
{
"teachers" : [
{
"firstName" : "Roxana",
"lastName" : "Stolniceanu"
}
]
}
]
}

示例 1:$pop:

global.db
.collection('students')
.updateOne(
{ _id : studentObjectId },
{ $pop: { "courses.1.teachers": 1 } } // 1 for last item, -1 for the first
)

示例 2:$pull:

global.db
.collection('students')
.updateOne(
{ _id : studentObjectId },
{ $pull: { "courses.1.teachers": { firstName: 'Code' } } }
)

关于node.js - 如何在 MongoDB 中 $unset 整个对象而不是将其保留为空或 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com