can't make basic mongo shell script with authentication

时间秒杀一切 提交于 2019-12-03 12:13:44

I finally made this work. This is how i ended up doing it:

First I made a file called test.js with the following in it:

db = connect("localhost:27017/admin");

db.auth('username','password');

db = db.getSiblingDB('test');

var cursor = db.cust.find();

while (cursor.hasNext()) {
   printjson(cursor.next());
}

I then ran this command from the command line:

mongo test.js

I also want to point out a few things that i learned while trying to do this to any other developer who is having issues.

1) if you add a new database, and your are running mongo with authentication you either need to log into the authentication database first and then switch to the desired database (as my example shows) or you need to add a user/password to the desired database (as i probably should have done in the first place)

2) When running a javascript file via mongo, don't expect to use the same "javascript" functions that you are used to. I just learned a hard lesson that not all javascript is the same. for example, you can not use Console.log() in a javascript file that is run via mongo because console.log is not actually core javascript but rather a function specific to browser and node implementations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!