I have two collections, campgrounds and reviews while deleting an review it stays in object array in campgrounds as an ObjectID ref and i can't find a way to delete them from there.
I tried
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") }}
I've had these results
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
or
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
or
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 2,
"errmsg" : "Cannot apply $pull to a non-array value"
}
})`
Campground Schema
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'
}
]
})