如何在mongdb程序中设置用户名和密码?

时间:2017-06-04 09:35:06

标签: mongodb

我需要如何在mongodb程序中设置用户名和密码。我试过它显示无法连接数据库。该网址链接中的错误是什么?

var mongodb=require("mongodb");
var mongoclient=mongodb.MongoClient;
var url="mongodb://viki:vino@localhost:27017/details";
mongoclient.connect(url,function(err,db)
{
    if(err)
    {
        console.log("unable to connect the database");
    }
    else
    {
        console.log("connect the details database successfully");
        var collection=db.collection("users");
        var user4= {
            "name":"princy",
            "age":21,
            "subject":["English","Grammar"],
            "address": {
                "city": "Rahim nagar",
                "state": "Tamilnadu",
                "pincode": "582028237"
            }
        };
        collection.insert(user4,function(err,result)
        {
            if(err)
            {
                console.log("not inserted");
            }
            else {
                console.log("%d documents inserted",result.length);
                console.log("Result:"+result);
            }
        });
        db.close();
    }
});

0 个答案:

没有答案