1exports.addFriend = function (req, res, next)
2{
3var friend = {"firstName": req.body.fName, "lastName": req.body.lName};
4Users.findOneAndUpdate({name: req.user.name}, {$push: {friends: friend}});
5};
1// With { $push: { field: element } }
2
3// Example:
4const elementToPush = { a: 1, b: 2 };
5const body = { $push: { arrayField: elementToPush } };
6model.patch(id, body);
7
1
2//mongoose appending data
3
4var objFriends = { fname:"fname",lname:"lname",surname:"surname" };
5Friend.findOneAndUpdate(
6 { _id: req.body.id },
7 { $push: { friends: objFriends } },
8 function (error, success) {
9 if (error) {
10 console.log(error);
11 } else {
12 console.log(success);
13 }
14 });
15)