Remove a specific id from all the collections in single query mongodb

吃可爱长大的小学妹 提交于 2019-12-13 21:03:59

问题


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:

  1. Remove the venue document using the _id value of the venue document. You can use findByIdAndRemove() which will also give you the deleted venue document.
  2. Using the _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.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!