I have Reviews collection
{
"_id" : ObjectId("5ac47af5e935d927a03518ac"),
"venue" : ObjectId("5abc9436b7c99332f0a68136"),
"content" : "gfhfghfghfghf",
}
I have Event collection
{
"_id" : ObjectId("5abce7208b9efa5a6b8e739b"),
"venue" : ObjectId("5abc9436b7c99332f0a68136"),
"description" : "22222222222222222222222222222222222222",
}
I have Venues collection
{
"_id" : ObjectId("5abc9436b7c99332f0a68136"),
"name" : "ASA College - Manhattan Campus",
"addedBy" : ObjectId("5abc93a85d3914318cc8d3d7"),
"__v" : 0,
}
How do I remove Reviews
and Events
on deleting its corrosponding Venue
It is quite difficult to create a single query that performs the deletion action for the collection you want to delete and its related collection. But, what you can do is:
_id
value of the venue
document. You can use findByIdAndRemove()
which will also give you the deleted venue
document. _id
of this venue
document you can query the Reviews
collection to delete all the Reviews
document that has _id
of venue
in its venue
property as a reference. sample code
Venue.findByIdAndRemove(idOfVenue, function (err, venueObj) {
if(err){
console.log('Error in deleting Venue');
} else {
Reviews.remove({venue: venueObj._id}, function (err) {
if (err) {
console.log('Error in deleting Reviews');
} else {
console.log('Record deleted successfully!!');
}
});
}
});
But however you can set up your schema in such a way that when you remove venue
then reviews
associated with that venue
is deleted from the schema level.
// Remove Venue
Venue.remove({_id: vanueId}, function (err) {
if (err) {
console.log('Error in removing venue');
}
});
//Remove Reviews related to venue, this code is placed in Venue schema
VenueSchema.pre('remove', function(next) {
this.model('Reviews').remove({ venue: this._id }, next);
});
Using pre
on remove
operation will first remove the record from Reviews
collection and then remove a record from Venue
collection. Thus, You have only one query that is to remove the Venue
record. Removing from Reviews
is handled at the schema level of Venue
.
MongoDB being a NoSQL database doesn't support relations instead alternatively it facilitates defining relational model using embedded documents.
According to above mentioned description,To delete all reviews and events associated to specific venue ,remove operation needs to executed into multiple steps with first step to execute remove operation to remove all reviews and events using filter of venue and then into second step perform operation to remove venue using filter of _id representing venue Id field
Alternatively events and reviews can be defined as an embedded document within venue document as mentioned into below example.
{
"_id": ObjectId("5abc9436b7c99332f0a68136"),
"name": "ASA College - Manhattan Campus",
"addedBy": ObjectId("5abc93a85d3914318cc8d3d7"),
"__v": NumberInt(0),
"reviews": [{
"content": "gfhfghfghfghf"
}],
"events": [{
"description": "22222222222222222222222222222222222222"
}]
}
The above document schema will facilitate delete operation of venue and its associated reviews and events into a single operation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.