MongoDB(Node.js)发现没有返回预期的结果

时间:2013-03-12 19:57:15

标签: node.js mongodb database

我正在尝试从节点到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(这里有一些数据),插入工作。只是找不到。

1 个答案:

答案 0 :(得分:4)

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

collection.find().toArray(callback)