简体   繁体   English

删除父猫鼬模式数组内的子模式中的单个行

[英]Removing the individual row in child schema inside the array of parent mongoose schema

i want to remove the child schema's row based on certain field and schema is inside the array of parent schema. 我想基于某些字段删除子模式的行,而模式位于父模式的数组内。 i have tried with below code but failed.can any one resolve this //code details use_list is parent schema in which its array field called frr_list has array element as schemas . 我试过下面的代码,但是失败了。任何人都可以解决这个问题//代码详细信息use_list是父模式,在该父模式中,其名为frr_list的数组字段具有数组元素作为schemas。 and i want to remove individual row of that child schema based on fren_id. 我想基于fren_id删除该子架构的单个行。 // //

 //main parent schema var user_list = new Schema({ user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'user_reg' }, fren_list: [fren_list], block_list: [fren_block_list], frr_list: [fern_req_recieved_list], //asking about this array frs_list: [fern_req_sent_list], }); //child schema var fern_req_recieved_list = new Schema({ //fren req list fren_id: { type: mongoose.Schema.Types.ObjectId, ref: 'user_reg' }, fren_name: String, dt: { type: Date, default: Date.now }, status: { type: String, default: "pending" } //pending, rejected,accepted }); User.list.findOne({ //parent document user_id: currentUserId }, function(err, user) { ///below is child inside the array field and is schema user.frr_list.findOneAndRemove({ fren_id: requestedUserId }, function(err, message) { if (err) { callback(null, false); } else { callback(null, true); } }) }); 

As I understand you can delete the individual items of frr_list array like this: 据我了解,您可以像下面这样删除frr_list数组的各个项目:

I assume that you will be deleting the individual items of frr_list by frr_id (which is _id which is generated by mongoose) 我假设您将通过frr_id(由猫鼬生成的_id)删除frr_list的各个项目

User.update({ _id: userListId, user_id:currentUserId //filter record by userListId and currentUserId },

{ //now pull the required row from the array based on _id of array $pull: { frr_list: { _id: frr_id } } }, function(err,result){ if(result.ok==1 && result.nModified==1 && result.n==1) //successfully deleted else if(result.ok==1 && result.nModified==0 && result.n==0) //no record exist else //something goes wrong });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM