作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的第一个 Web 应用程序,我需要删除一个嵌套的数组项。您将如何使用此模式删除 Mongoose 中的对象:
User: {
event: [{_id:12345, title: "this"},{_id:12346, title:"that"}]
}
如何删除 mongoose/Mongo 中的 id:12346
?
最佳答案
使用$pull从下面的项目数组中删除项目:
db.User.update(
{ },
{ $pull: { event: { _id: 12346 } } }
)
The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
第一个参数中的空对象是用于查找文档的query
。上面的方法删除了集合中所有文档中event
数组中_id:12345
的项。如果数组中有多个项目符合条件,请将 multi
选项设置为 true,如下所示:
db.User.update(
{ },
{ $pull: { event: { _id: 12346 } } },
{ multi: true}
)
关于javascript - 如何从 Mongoose 中的数组中删除嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41430675/
我是一名优秀的程序员,十分优秀!