如何删除 MongoDB 字段中的元素
{
_id: "d473ad718f214f158b31d591a97a5ce7",
name: 'sam',
keys: {
q8f3ff57b03940959e1ae4dfe2cb57ad: {
serviceaccountkey: {
keyID: 'q8f3ff57b03940959e1ae4dfe2cb57ad',
status: 'ACTIVE',
keytype: 2,
}
},
m887f7854adb4907b57903b896bcf6d6: {
serviceaccountkey: {
keyID: 'm887f7854adb4907b57903b896bcf6d6',
status: 'ACTIVE',
keytype: 2,
}
}
}
}
}
假设这是一个文档。如何从此文档中删除 ID 为“q8f3ff57b03940959e1ae4dfe2cb57ad”的第一个密钥?我希望最终结果看起来像:
{
_id: "d473ad718f214f158b31d591a97a5ce7",
name: 'sam',
keys: {
m887f7854adb4907b57903b896bcf6d6: {
serviceaccountkey: {
keyID: 'm887f7854adb4907b57903b896bcf6d6',
status: 'ACTIVE',
keytype: 2,
}
}
}
}
}
{
_id: "d473ad718f214f158b31d591a97a5ce7",
name: 'sam',
keys: {
q8f3ff57b03940959e1ae4dfe2cb57ad: {
serviceaccountkey: {
keyID: 'q8f3ff57b03940959e1ae4dfe2cb57ad',
status: 'ACTIVE',
keytype: 2,
}
},
m887f7854adb4907b57903b896bcf6d6: {
serviceaccountkey: {
keyID: 'm887f7854adb4907b57903b896bcf6d6',
status: 'ACTIVE',
keytype: 2,
}
}
}
}
}
Suppose this is a document. How do I remove the first key, with ID "q8f3ff57b03940959e1ae4dfe2cb57ad" from this document? I want the end result to look like:
{
_id: "d473ad718f214f158b31d591a97a5ce7",
name: 'sam',
keys: {
m887f7854adb4907b57903b896bcf6d6: {
serviceaccountkey: {
keyID: 'm887f7854adb4907b57903b896bcf6d6',
status: 'ACTIVE',
keytype: 2,
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

使用$unset:
Use $unset: