简体   繁体   中英

Adding Users to an array in existing Mongodb collection - Mongodb

I have a collection named "tasks" .

var newTask = {
            name: req.body.newTask,
            room: req.body.newRoom,
            users: [req.body.newUser]
        };

I want to insert users in tasks. For example task named "Session" can have multiple users.

{
name: Session,
room: ElementRoom
users: [steve, john]
}

What I'm doing is something like this:-

app.post('/room/task/user', function(req, res, next){
    db.tasks.update(
    { name: req.body.taskName },
    { $push: { users: req.body.username } }
    )
});

But I'm getting this error:- TypeError: Cannot read property 'update' of undefined

what am i doing wrong?

Thanks a lot in advance

This is the solution:-

app.post('/room/task/user', function(req, res, next){
db.collection('tasks', function(err, tasksCollection){
    tasksCollection.update(
    { name: req.body.taskName },
    { $push: { users: req.body.username } }
    )
    console.log(req.body);
});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM