Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

javascript - how to connect mongoDB to server?

I am trying to get data from mongoDB while connecting to server .i inserted one value in mongoDB like this

> use abc


switched to db abc
> db.ac.insert({name:"naveen"})
WriteResult({ "nInserted" : 1 })
> show collections
ac
system.indexes 

And try to get that value like 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  to server.");

    MongoClient.connect(url, function(err, db) {
        assert.equal(null, err);
        var collection = db.collection('ac');
        console.log(collection)
        console.log("Connected correctly to server.");
        db.close();
    });
})

var url = 'mongodb://localhost:27017/abc';


app.listen(3000,function(){
         console.log('server is runninngn')
})

I am getting this error why ?

MacBook-Pro:expressjs naveenkumar$ node index.js
server is runninngn
Connected  to 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 to server.
Connected  to 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 to server.**

Please why it is giving error

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to display all the documents in the given collection the code should be like this :

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 to server.");
db.close();
});

this will print all the documents of collection ac. Otherwise if you directly try to print collection all the metadata will also get printed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...