DennisTurn
DennisTurn

Reputation: 139

Running a pure Mongo command within Node/Express/Mongoose?

How do I run the following command within my Node application?

db.users.update(
    { "username": "steve" },
    {  $push: 
        { 
            likes: [ { "user_id": "3" } ]
        } 
    } 
)

I have a schema setup via Mongoose but I'm not sure if it's possible to run this raw Mongo command.

Upvotes: 0

Views: 146

Answers (1)

ayushgp
ayushgp

Reputation: 5091

Updating docs using Mongoose should help you. The following should help you update docs using mongoose:

Model.update(
    {"username":"steve"},
    {$push: {
            likes: [ { "user_id": "3" } ]
        }
    },
    //if you have any options mention here(options),
    //A callback comes here (optional)
)

Here is the syntax of update command using Mongoose for your reference:

var conditions = { name: 'borne' }
  , update = { $inc: { visits: 1 }}
  , options = { multi: true };

Model.update(conditions, update, options, callback);

function callback (err, numAffected) {
  // numAffected is the number of updated documents
})

Upvotes: 1

Related Questions