I want to add a new field to a an object after i search the specific object, Database looks like this:
{
"_id": {
"$oid": "608378ef1ae6b1368c6220c6"
},
"username": "paul",
"password": "cacavaca123",
"question": "q1",
"answer": "cutu"
}
{
"_id": {
"$oid": "6084c1b557f2242bcc629440"
},
"username": "mircea",
"password": "123456",
"question": "q1",
"answer": "cutu"
}
And i want to search in db for the object with username: paul and add a new field friends:[alex], I have tried this but its not working:
MongoClient.connect(uri, function(err, db) {
var dbc = db.db("chat");
dbc.collection("accounts").find({username: { $eq: user}}).aggregate( [
{
$addFields: {
frequent:[new_friend]
}
}
])
db.close();
});
Also if you know, can you tell me how to update that array of friends and add a new friend to that array?