node.js - 如何删除 MongoDB 中的 ObjectID 数组?
问题描述
我有两个集合,露营地和评论,同时删除评论时它作为 ObjectID 参考保留在露营地的对象数组中,我找不到从那里删除它们的方法。
我试过了
db.campgrounds.update({ title: "Ancient Hunting Camp" }, { $pull:{id:"60f8293044b0cc40c409f2f7"}})
db.campgrounds.update({ title: "Ancient Hunting Camp" }, { $pull: {reviews:{id:"60f8293044b0cc40c409f2f7"}}})
db.campgrounds.update({ title: "Ancient Hunting Camp" }, { $pullAll: {reviews:[]}},{multi:true})
db.campgrounds.update({ title: "Ancient Hunting Camp" }, { $pull: { reviews: {}}}, {multi:true})
db.campgrounds.update({ _id: "603d7a10f969af31484fb56e" }, { $pull: { reviews: "ratings"}})
db.campgrounds.update({ _id: "603d7a10f969af31484fb56e" }, { $pull: { reviews: ObjectId("60f976cd6132a91a20bd6679") }}
我有这些结果
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
或者
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
或者
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 2,
"errmsg" : "Cannot apply $pull to a non-array value"
}
})`
露营地架构
const CampgroundSchema = new Schema({
title: String,
image: String,
price: Number,
description: String,
location: String,
tel: Number,
website: String,
email: String,
reviews: [
{
type: Schema.Types.ObjectId,
ref: 'Review'
}
]
})
解决方案
推荐阅读
- php - 如何在 Lumen 中使用外部 Auth API 验证 Token
- r - 闪亮的自定义按钮
- c++ - 如何处理“Cmake 错误:无法找到请求的 Boost 库”?
- r - 使用 sjlabelled::val_labels() 进行准引用
- php - 我在登录控制器中设置会话并在仪表板模型中接收也在自动加载中加载会话
- rust - 确保 rust 中的 trait 实现满足属性
- python-3.x - Python3 - 如何以相同的字符串结尾导入所有内容
- php - 自定义表单验证由于某种原因无法正常工作
- javascript - 如何根据fillText()宽度确定准确的fillRect()宽度?
- java - 无法通过同意页面