Irene Liu
Irene Liu

Reputation: 17

Mongojs: findOne() doesn't work

I was trying to use findOne method. But it didn't show anything.It looks like it didn't execute. Would you like to help me solve this problem?

var mongojs = require('mongojs');

var databaseUrl = "mongodb:local:27017/mydb";
var db = mongojs(databaseUrl, ["profiles"]);

var password;

db.profiles.findOne({"userId": "liu1234"}, function(err, doc) {
    if (err) throw err;
    else console.log(doc);
});

Upvotes: 0

Views: 746

Answers (1)

natedog
natedog

Reputation: 141

The format of databaseUrl is incorrect. The mongodb driver is unable to find your database.

Try: var databaseUrl = "mongodb://localhost:27017/mydb";

The first part, mongodb://, refers to the protocol that mongodb uses to interact with the database. The next part, localhost , is a hostname that points to your machine. :27017 refers to the default port that mongodb communicates over. And, obviously, /mydb refers to your database.

If you're using a default configuration, you don't even need to specify the protocol, the host, or the port. Mongojs assumes the defaults if you don't enter them, so you can use this instead:

var databaseUrl = "mydb";

For more information check out: https://github.com/mafintosh/mongojs

Upvotes: 2

Related Questions