1

我正在尝试从节点到 mongodb 服务器进行查找。这是代码:

var mongo = require('mongodb'),
    Server = mongo.Server,
    Db = mongo.Db;
var mongoServer = new Server(10.141.74.74, 28019,{auto_reconnect: true, safe:true});
var db = new Db("statusReport", mongoServer, {safe:true});

db.open(function(err,db){
        if(!err){
            db.collection("status",function(err,collection){
                if(!err){
                    collection.find({}, function(a,b){
                        console.log(a);
                        console.log(b);
                    });
                }else{
                    console.log("ERROR: could not open collection");
                }
            });
            db.close();
        }else{
            console.log("ERROR: could not open database");
        }
    });

但是,它不是返回数据而是返回:

{ db:
   { domain: null,
     _events: { open: [] },
     _maxListeners: 10,
     databaseName: 'statusReport',
     serverConfig:
      { domain: null,
        _events: null,
        _maxListeners: 10,
        host: '10.141.74.74',
        port: 28019,
        options: [Object],
        internalMaster: true,
        connected: true,
        poolSize: 5,
        disableDriverBSONSizeCheck: false,
        ssl: false,
        slaveOk: undefined,
        _used: true,
        _readPreference: null,
        socketOptions: [Object],
        logger: [Object],
        eventHandlers: [Object],
        _serverState: 'connected',
        _state: [Object],
        recordQueryStats: false,
        dbInstance: [Circular],
        dbInstances: [Object],
        connectionPool: [Object],
        isMasterDoc: [Object] },
     options: { safe: true },
     _applicationClosed: false,
     native_parser: undefined,
     bsonLib:
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey] },
     bson: {},
     bson_deserializer:
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey] },
     bson_serializer:
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey] },
     _state: 'connected',
     pkFactory:
      { [Function: ObjectID]
        index: 0,
        createPk: [Function: createPk],
        createFromTime: [Function: createFromTime],
        createFromHexString: [Function: createFromHexString] },
     forceServerObjectId: false,
     safe: true,
     notReplied: {},
     isInitializing: true,
     auths: [],
     openCalled: true,
     commands: [],
     _callBackStore: { domain: null, _events: {}, _maxListeners: 10, _notReplied: {} },
     logger: { error: [Function], log: [Function], debug: [Function] },
     slaveOk: false,
     tag: 1363118044637,
     eventHandlers:
      { error: [],
        parseError: [],
        poolReady: [],
        message: [],
        close: [] },
     serializeFunctions: false,
     raw: false,
     recordQueryStats: false,
     reaperEnabled: false,
     _lastReaperTimestamp: 1363118044637,
     retryMiliSeconds: 1000,
     numberOfRetries: 60,
     reaperInterval: 10000,
     reaperTimeout: 30000,
     readPreference: undefined },
  collection:
   { db:
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        databaseName: 'statusReport',
        serverConfig: [Object],
        options: [Object],
        _applicationClosed: false,
        native_parser: undefined,
        bsonLib: [Object],
        bson: {},
        bson_deserializer: [Object],
        bson_serializer: [Object],
        _state: 'connected',
        pkFactory: [Object],
        forceServerObjectId: false,
        safe: true,
        notReplied: {},
        isInitializing: true,
        auths: [],
        openCalled: true,
        commands: [],
        _callBackStore: [Object],
        logger: [Object],
        slaveOk: false,
        tag: 1363118044637,
        eventHandlers: [Object],
        serializeFunctions: false,
        raw: false,
        recordQueryStats: false,
        reaperEnabled: false,
        _lastReaperTimestamp: 1363118044637,
        retryMiliSeconds: 1000,
        numberOfRetries: 60,
        reaperInterval: 10000,
        reaperTimeout: 30000,
        readPreference: undefined },
     collectionName: 'status',
     internalHint: null,
     opts: {},
     slaveOk: false,
     serializeFunctions: false,
     raw: false,
     readPreference: 'primary',
     pkFactory:
      { [Function: ObjectID]
        index: 0,
        createPk: [Function: createPk],
        createFromTime: [Function: createFromTime],
        createFromHexString: [Function: createFromHexString] } },
  selector: {},
  fields: undefined,
  skipValue: 0,
  limitValue: 0,
  sortValue: undefined,
  hint: null,
  explainValue: undefined,
  snapshot: undefined,
  timeout: true,
  tailable: undefined,
  awaitdata: undefined,
  numberOfRetries: 5,
  currentNumberOfRetries: 5,
  batchSizeValue: 0,
  slaveOk: false,
  raw: false,
  read: 'primary',
  returnKey: undefined,
  maxScan: undefined,
  min: undefined,
  max: undefined,
  showDiskLoc: undefined,
  comment: undefined,
  tailableRetryInterval: 100,
  exhaust: false,
  partial: false,
  totalNumberOfRecords: 0,
  items: [],
  cursorId: { _bsontype: 'Long', low_: 0, high_: 0 },
  dbName: undefined,
  state: 0,
  queryRun: false,
  getMoreTimer: false,
  collectionName: 'statusReport.status' }

我知道里面有东西,因为我可以使用 MongoHub 看到它。我也可以在那里查询。这里有什么问题?

我也尝试做 collection.insert(some data here) 并且插入工作。只是发现没有。

4

1 回答 1

4

collection.find(callback)返回光标,而不是文档。你想做:

collection.find().toArray(callback)
于 2013-03-12T20:48:45.460 回答