如何将mongoDB连接到服务器?

时间:2015-10-31 03:43:22

标签: javascript node.js mongodb express mongodb-query

我试图在连接到服务器时从mongoDB获取数据.i在mongoDB中插入一个值,如下所示

> use abc


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

并试着像这样获得这个价值

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')
})

我收到此错误的原因是什么?

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.**

请给出错误的原因

1 个答案:

答案 0 :(得分:0)

如果要显示给定集合中的所有文档,代码应如下所示:

cat /bin/ls |xxd -g 1 -i | xxd -r
xxd: sorry, cannot seek backwards.

这将打印收集ac的所有文件。否则,如果您直接尝试打印集合,所有元数据也将被打印。