Sunday, 11 August 2013

insterting documents in mongoDB using node.js

insterting documents in mongoDB using node.js

i have following code of node.js where by it connects to mongoDB and i
would like to insert the message displayed at node.js console in to
collection called "mytable". The problem i am facing is it is successfully
connecting to mongoDB and it also inserts the message which is displaying
at node.js console but instead of insterting as one full document it is
inserting as characters. why it is so happening? pls help
var mongo = require("mongodb");
var dbhost = "127.0.0.1";
var dbport = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db("mydb", new mongo.Server(dbhost, dbport, {}), {safe:
true});
db.open(function(error){
status = ("db connected at" + dbhost + " : " + dbport);
console.log(status);
db.collection("mytable",function (error,collection){
collection.insert(status, function(error){
if (error){
console.log("Error : ", error.message);
} else {
console.log("Inserted in to database");
}
});
});
});
my node console display output as
db connected at127.0.0.1 : 27017
Inserted in to database
while checking 'mytable' mongo shell with command
db.mytable.find.forEach(printjson)
it is showing following output
{
"_id" : objectId("5207741d193770459f068317"),
"0" : "d",
"1" : "b",
"3" : " ",
"4" : "c",
"5" : "o",
"6" : "n",
"7" : "e",
"8" : "c",
"9" : "t",
"10" : "e",
"28" : "7",
"29" : "0",
"30" : "1",
"31" : "7"
}
Please help i want only one entry i.e. 'db connected at 127.0.0.1 : 27017'
to be instertd in to mytable as single document.

No comments:

Post a Comment