Tuesday, 11 April 2017

select recently inserted data and push into a referenced array

Schema that I used

var candidateSchema = new mongoose.Schema({
    name : String,
    partyName: {type: String, unique: true},
    information: String
});

var candidate = mongoose.model("candidate", candidateSchema);

var adminSchema = new mongoose.Schema({
    electionName : {type: String, unique: true},
    endDate      : {type: Date},
    candidates   : [{type: mongoose.Schema.Types.ObjectId, 
                    ref: "candidate"}]  
});

var addElection = mongoose.model("addElection", adminSchema);

Here, whenever i push the data to candidates[] array , all data gets saved in the top most row of addElection.

Here is my code:

candidate.create(req.body.stepTwo, function(err, candid){
   if(err){
       console.log(err);
   } else {
        console.log(candid);
        addElection.update({}, {$push:{ candidates: candid}}, function(err){
        });

I want to push the data to candidates[] array,in such way that new data gets saved in the next row of addElection/(in new election).



via Abhijeet Baviskar

No comments:

Post a Comment