Sunday, 19 March 2017

mongoose, nodejs how to add items into mongo array

I have a collection like { id:"david123", friends[{id:joe321, lname"woo", fname"joe"}] }

i want to add new elements into friends

i currently have this, but it does not seem to be working

app.post('/users/:uid/friends', function(req, res){
  var userId = req.params.uid;
  Friend.update({'_id': userId},
    {$push: {Friends: req.body.friend}},
    { upsert : true },
    function(err, result){
      if (err){
    console.log(err);
  } else {
    res.status(200).json(result);
  }
  })
});

i defined my schema like this

var FriendSchema = new mongoose.Schema({
  _id: String,
  Friends: [{
    _id: String,
    fname: String,
    lname: String
  }]
});



via SanuoXeu

No comments:

Post a Comment