简体   繁体   中英

Mongoose Subdocument array pushing

My scenario is if person1 accepting person2 deal means..the person1_id will save inside that person2 particular deal field accepted ,i have tried the code it was working perfectly if a accepted user(person2) has one deal but in case of more than one deal it was updating but deleting other deals (ie,the suppose the person2 having 3 deals means if person1 accepting 3rd deal the accepted user id was updating in 3rd deal and the 1st and 2nd deal was deleted).Anyone please help me how to save only the updated deal array

var incomingUser = req.user;//accepting user accesstoken in header(person1) 
if(req.params.id){
var id = req.params.id;//deal id
console.log("DealId:"+id + "Acceptinguser:"+incomingUser.name);
User.findOne(
{
    "deals": {
      $elemMatch: {
        _id: id
      }
    }
  },
function(err, data){
  console.log("Dealer:" +data.name);
  console.log("deal:"+ data.deals);
  if(err){
    console.log("User not found");
    res.send(new restify.ResourceNotFoundError('failed','Deal not found'));
    return next();
  }
    var dealObj = _.filter(data.deals, { id: id })[0];
    console.log("Deal Obj" + dealObj);
     var acceptingUser = incomingUser;
     console.log("accepting user:" +acceptingUser._id);
     dealObj.accepted = acceptingUser._id;
     console.log("accept id: "+ dealObj.accepted);
     data.deals = dealObj;
     console.log("data:"+ data.deals);
  data.save(function (err, result){
    console.log("Result:" + result);
    if(err){
      console.log("Internal error");
      res.send(new restifyc.InternalError('failed','Error accepting'));
      return next();
    }
    console.log("saved");
    res.send(200,{user: result});
    return next();
  });
});
}
}

And my schema is

var dealSchema = new mongoose.Schema({
shopName: {type: String,required: true},
deal: {type: String,required: true},
price:{type: Number,required: true},
start:{type: Date,default: Date.now},
end:{type: Date},
expiry:{type: Date},
comments:{type: String},
accepted: {type:mongoose.Schema.Types.ObjectId, ref:'user'},//person1 _id
rejected: {type:mongoose.Schema.Types.ObjectId, ref: 'user'}
});
var userSchema = new mongoose.Schema({
  name: { type: String,required: true},
  phone: { type: Number, required: true,unique: true},
  email:{type: String},
  password: {type: String},
  deals:[dealSchema]
  }, {collection: 'user'});
  mongoose.model('Deal', dealSchema);
  mongoose.model('user', userSchema);

Yep in order to update specifically what you need you can use the <array>.$ for the specified position of the element:

User.update(
  "deals": {
    $elemMatch: {
      _id: id
    }
  }, {
    "$set": {
      "deals.$" : {/*your deal data*/}
    }
  }, function(err, doc) {

  });

More details on how to use the $ wildcard https://docs.mongodb.org/manual/reference/operator/update/positional/

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.

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