I’m using Nodejs to insert some data via MongoDB as so:
var db = new mongo.Db("mydb", new mongo.Server("localhost", '27017', {}), {});
db.open(function() {
db.collection("entry", function(error, collection) {
data = {
"time":(new Date()).getTime()
}
collection.insert(data, function() {
console.log("saved", arguments);
});
});
});
However, I’m not even seeing the database if I do:
$ mongo localhost
> db.getCollectionNames();
[ "system.indexes" ]
So I manually added the collection via:
> db.createCollection('mydb');
{ "ok" : 1 }
Then I see:
> db.getCollectionNames();
[ "mydb", "system.indexes" ]
but if I run:
> db.mydb.find().count()
I’ll see: 0
Of course I’ll try db.mydb.find().count() after I run my node script and still I get 0. I suspect I’m missing a vital piece somewhere.
I should also note that when I run my node script, I do see “saved” and some args from the function so it seems like data is going in, I’m just not sure how to see it.
It looks like you are conflating the concepts of databases and collections in your example code and problem description. Rather than db.getCollectionNames(), use this for finding the databases available on the MongoDB server:
Your code is adding a record to the “entry” collection in the “mydb” database. After connecting to the mongo shell, try:
Then:
Which should display your newly created records.
You can check http://www.mongodb.org/display/DOCS/DBA+Operations+from+the+Shell for a full list of useful shell commands.