问题
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
回答1:
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:
- Remove the venue document using the
_id
value of thevenue
document. You can usefindByIdAndRemove()
which will also give you the deletedvenue
document. - Using the
_id
of thisvenue
document you can query theReviews
collection to delete all theReviews
document that has_id
ofvenue
in itsvenue
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
.
回答2:
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.
来源:https://stackoverflow.com/questions/49705054/remove-a-specific-id-from-all-the-collections-in-single-query-mongodb