i am having a problem while following a tutorial in node.js using mongodb database with monk. When i run my database command in the mongo console i can see that my database nodetest1 has 3 entries, however when i am doing this it returns nothing.
router.get('/', function(req, res, next) {
var db = req.db;
var collection = db.get('usercollection');
collection.find({},{},function(e,docs){
res.send(docs);
});
});
And here is a print from the mongo console when i use the find method.
> db.usercollection.find().pretty()
{
"_id" : ObjectId("55b17a551efb190c06c38d31"),
"username" : "testuser1",
"email" : "testuser1@testdomain.com"
}
{
"_id" : ObjectId("55b17a5f1efb190c06c38d32"),
"username" : "testuser2",
"email" : "testuser2@testdomain.com"
}
{
"_id" : ObjectId("55b17a5f1efb190c06c38d33"),
"username" : "testuser3",
"email" : "testuser3@testdomain.com"
}
If you need more information i will give it to you. I hope you can help me, i have been stuck on this problem for 45 mins trying to find a solution.
UPDATE: This is how is started the mongod
mongod --dbpath "C:\Users\Victor\OneDrive\Programming\NodeJS\DatabaseSetup\data"
And my connection to the database is in the app.js:
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/DatabaseSetup');
app.use(function(req,res,next){
req.db = db;
next();
});
When i run my program at localhost:3000/userlist i get no data from the database but the console writes this:
2015-07-24T03:13:28.127+0200 I NETWORK [initandlisten] connection accepted from 127.0.0.1:57961 #151 (6 connections now open)
Going by the information you have listed above I'm willing to chance that database connection strings do not work in the way you think they do.
You say youself you are starting an instance of the mongod
server like this:
mongod --dbpath "C:\Users\Victor\OneDrive\Programming\NodeJS\DatabaseSetup\data"
Then you can connect to the shell, i.e:
mongo
And all is fine.
The problem here is that the "database" you are actually connecting to by default and without other declaration is called "test" and is not the name as the path in where you store the physical data files.
Therefore to connect monk to that data then do:
var db = require('monk')('localhost/test');
Which is connecting to the same database name as where the data is stored.
If you wanted to create a different database name then do:
mongo
> use DatabaseSetup
Or just from the command line:
mongo DatabaseSetup
In either case this switches the default database contained. Then you can create your data in that namespace.
But otherwise your data likely resides in "test", so that is what you should be using.
Just re-read your first sentence there. Perhaps the database container name is "nodetest1" since that what you state, therefore:
var db = require('monk')('localhost/nodetest1');