Category Archives: Stack Overflow

How to create a user in MongoDB v3.0.5

I need to create a user for my database in mongodb, but it seems that I can't get it to work.

I have installed mongoDb v3.0.5 on my windows 7 machine. according to this article, I connected to my mongo instance using:

mongo -u siteUserAdmin -p password

and then I created first user via:

use admin
db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

then I created a user for my nodejs application on exampleDb:

use admin
db.createUser(
  {
    user: "myUser",
    pwd: "123456",
    roles: [ { role: "readWrite", db: "exampleDb" } ]
  }
)

and finally in my nodejs application, when I try to connect to my mongo instance using this connection string:

var connString = "mongodb://myUser:123456@127.0.0.1:27017/exampleDb";

an Authentication failed error happens:

{ name: 'MongoError',
  message: 'Authentication failed.',
  ok: 0,
  code: 18,
  errmsg: 'Authentication failed.'
}

How to create a user in MongoDB v3.0.5

I need to create a user for my database in mongodb, but it seems that I can't get it to work.

I have installed mongoDb v3.0.5 on my windows 7 machine. according to this article, I connected to my mongo instance using:

mongo -u siteUserAdmin -p password

and then I created first user via:

use admin
db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

then I created a user for my nodejs application on exampleDb:

use admin
db.createUser(
  {
    user: "myUser",
    pwd: "123456",
    roles: [ { role: "readWrite", db: "exampleDb" } ]
  }
)

and finally in my nodejs application, when I try to connect to my mongo instance using this connection string:

var connString = "mongodb://myUser:123456@127.0.0.1:27017/exampleDb";

an Authentication failed error happens:

{ name: 'MongoError',
  message: 'Authentication failed.',
  ok: 0,
  code: 18,
  errmsg: 'Authentication failed.'
}

How to extend q.promise in node.js?

I need to extent q.promise with my own functions, I tried:

defer = q.defer();
defer.promise.prototype.funcName = function () {
    // my function
}

witch returns following error:

TypeError: Cannot set property 'funcName' of undefined

How should I properly extend q.promise?


This is my actual problem: I need an alias for .then() witch gets name of an operation as argument (instead of a function), and executes that operation according to a internal library of functions.

Preserve `this` with recursive setImmediate()

Salam (means Hello:))

In my node.js app, I need to use setImmediate() to recessively call a function and keep its context intact for next execution.

Consider following example:

var i=3;

function myFunc(){
    console.log(i, this);
    --i && setImmediate(arguments.callee);
}

myFunc();

Output:

3 // a regular `this` object
2 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }
1 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }

As you can see, after first execution this is overwritten. How should I work around this?

Preserve `this` with recursive setImmediate()

In my node.js app, I need to use setImmediate() to recessively call a function and keep its context intact for next execution.

Consider following example:

var i=3;

function myFunc(){
    console.log(i, this);
    --i && setImmediate(arguments.callee);
}

myFunc();

Output:

3 // a regular `this` object
2 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }
1 { _idleNext: null, _idlePrev: null, _onImmediate: [Function: myFunc] }

As you can see, after first execution this is overwritten. How should I work around this?

how to update fields of a nested object in mongodb?

Salam (means Hello) :)

I'm using node-mongodb-native driver and I need to update specific fields of objects stored in an array. this is a sample document in my mongodb collection:

{
    "fields" : [ 
        {
            "en" : "birthDate",
            "status" : "enable",
            "index" : 10
        },{
            "en" : "email",
            "status" : "enable",
            "index" : 4
        },{
            "en" : "inviterCode",
            "status" : "enable",
            "index" : 2
        }
    ]
}

and this is my new data:

var newData = [ 
    {
        "en" : "birthDate",
        "status" : "disable",
    },{
        "en" : "email",
        "status" : "disable",
    }
];

as you can see, if en field matches, status field should be updated. I know that following query works for a single update, but how can I achieve multiple updates with a single query?

collection.update(
    {'fields.en': 'birthDate'}, 
    {$set:{'fields.$.status': 'disable'}}, 
    {w:1, multi: true}, 
    function(error, count){

    }
);