0

I have a nodeJS app and im using mongoDB to store some data like: 1. usrID, 2. usrName 3. Friends list[is an array]

using nodeJS how do i go to the collection "users" and then add to the array of someone with a specific usrID

This is some code that I have:

exports.makeFriendRequest = function(from, to){


mongo.connect(mongoDB, function(err, db) {

if (err) throw err;
var dbo = db.db("My database");
var from = from;
var to = to; //the userID array that we want to change

var collection = dbo.collection('users');

collection.insert(obj);

//dont know what to do here to add to the array


 });


}

i have an array with friends for each user and i want to be able to add a string which is another userID to represent that they are "friends" how would i do that?

0

1 Answer 1

2

Let's assume you need to add 89 to user 63's document. And your Friends list (array) field is frields_list and your user id field is userId

const updateResponse = await collection.updateOne(
    { userId: 63 },
    { $push: { frields_list: 89 } },
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.