javascript - how to connect mongoDB to server? -
i trying data mongodb while connecting server .i inserted 1 value in mongodb this
> use abc switched db abc > db.ac.insert({name:"naveen"}) writeresult({ "ninserted" : 1 }) > show collections ac system.indexes
and try value this
var express=require('express'); var app =express(); var mongoclient = require('mongodb').mongoclient; var assert = require('assert'); app.get('/',function(req,res){ console.log("connected server."); mongoclient.connect(url, function(err, db) { assert.equal(null, err); var collection = db.collection('ac'); console.log(collection) console.log("connected correctly server."); db.close(); }); }) var url = 'mongodb://localhost:27017/abc'; app.listen(3000,function(){ console.log('server runninngn') })
i getting error why ?
macbook-pro:expressjs naveenkumar$ node index.js server runninngn connected server. { s: { pkfactory: { [function: objectid] index: 462263, createpk: [function: createpk], createfromtime: [function: createfromtime], createfromhexstring: [function: createfromhexstring], isvalid: [function: isvalid], objectid: [circular], objectid: [circular] }, db: { domain: null, _events: {}, _maxlisteners: 10, s: [object], serverconfig: [getter], buffermaxentries: [getter], databasename: [getter], options: [getter], native_parser: [getter], slaveok: [getter], writeconcern: [getter] }, topology: { domain: null, _events: [object], _maxlisteners: 10, s: [object], bson: [getter], ismasterdoc: [getter], poolsize: [getter], autoreconnect: [getter], host: [getter], port: [getter], emitopen: false, connecttimeoutms: 30000, sockettimeoutms: 0 }, dbname: 'abc', options: { promiselibrary: [object], readconcern: undefined }, namespace: 'abc.ac', readpreference: { _type: 'readpreference', mode: 'primary', tags: undefined }, raw: undefined, slaveok: true, serializefunctions: undefined, internalhint: null, collectionhint: null, name: 'ac', promiselibrary: { [function: lib$es6$promise$promise$$promise] all: [function: lib$es6$promise$promise$all$$all], race: [function: lib$es6$promise$promise$race$$race], resolve: [function: lib$es6$promise$promise$resolve$$resolve], reject: [function: lib$es6$promise$promise$reject$$reject] }, readconcern: undefined } } connected correctly server. connected server. { s: { pkfactory: { [function: objectid] index: 462263, createpk: [function: createpk], createfromtime: [function: createfromtime], createfromhexstring: [function: createfromhexstring], isvalid: [function: isvalid], objectid: [circular], objectid: [circular] }, db: { domain: null, _events: {}, _maxlisteners: 10, s: [object], serverconfig: [getter], buffermaxentries: [getter], databasename: [getter], options: [getter], native_parser: [getter], slaveok: [getter], writeconcern: [getter] }, topology: { domain: null, _events: [object], _maxlisteners: 10, s: [object], bson: [getter], ismasterdoc: [getter], poolsize: [getter], autoreconnect: [getter], host: [getter], port: [getter], emitopen: false, connecttimeoutms: 30000, sockettimeoutms: 0 }, dbname: 'abc', options: { promiselibrary: [object], readconcern: undefined }, namespace: 'abc.ac', readpreference: { _type: 'readpreference', mode: 'primary', tags: undefined }, raw: undefined, slaveok: true, serializefunctions: undefined, internalhint: null, collectionhint: null, name: 'ac', promiselibrary: { [function: lib$es6$promise$promise$$promise] all: [function: lib$es6$promise$promise$all$$all], race: [function: lib$es6$promise$promise$race$$race], resolve: [function: lib$es6$promise$promise$resolve$$resolve], reject: [function: lib$es6$promise$promise$reject$$reject] }, readconcern: undefined } } **connected correctly server.**
please why giving error
if want display documents in given collection code should :
mongoclient.connect(url, function(err, db) { assert.equal(null, err); var collection = db.collection('ac').find({},function(err,doc){ if(err) throw err; else{ console.log(doc); } }); console.log("connected correctly server."); db.close(); });
this print documents of collection ac. otherwise if directly try print collection metadata printed.
Comments
Post a Comment