mongodb ? db.collection.find() ???????

Posted

技术标签:

【中文标题】mongodb 中 db.collection.find() 的返回类型是啥【英文标题】:What is return type of db.collection.find() in mongodbmongodb 中 db.collection.find() 的返回类型是什么 【发布时间】:2015-05-20 17:09:39 【问题描述】:

我正在尝试从 mongodb 连接中读取 find 函数的输出。

我想将数据库的结果存储到称为结果的 JSON/Array 中:

collection= db.collection('users');
result =collection.find().

现在有了这段代码,我得到了一些奇怪的长 json 输出:

 db: 
 domain: null,
 _events: ,
 _maxListeners: 10,
 databaseName: 'askr',
 serverConfig: 
   domain: null,
    _events: [Object],
    _maxListeners: 10,
    auth: [Getter],
    _callBackStore: [Object],
    _commandsStore: [Object],
    _dbStore: [Object],
    options: [Object],
    _serverState: 'connected',
    _haProcess: [Object],
    servers: [Object],
    strategyInstance: [Object],
    emitOpen: false,
    _state: [Object],
    _currentServerChoice: 0,
    socketTimeoutMS: [Getter/Setter],
    _used: true,
    logger: [Object] ,
 options: 
   read_preference_tags: null,
    read_preference: 'primary',
    url: 'mongodb://vishal:admin@ds055680.mongolab.com:55680/askr',
    native_parser: true,
    readPreference: [Object],
    safe: false,
    w: 1 ,
 _applicationClosed: false,
 slaveOk: false,
 bufferMaxEntries: -1,
 native_parser: true,
 bsonLib: 
   BSON: [Object],
    Long: [Object],
    ObjectID: [Object],
    DBRef: [Object],
    Code: [Object],
    Timestamp: [Object],
    Binary: [Object],
    Double: [Object],
    MaxKey: [Object],
    MinKey: [Object],
    Symbol: [Object] ,
 bson:  promoteLongs: true ,
 bson_deserializer: 
   Code: [Object],
    Symbol: [Object],
    BSON: [Object],
    DBRef: [Object],
    Binary: [Object],
    ObjectID: [Object],
    Long: [Object],
    Timestamp: [Object],
    Double: [Object],
    MinKey: [Object],
    MaxKey: [Object],
    promoteLongs: true ,
 bson_serializer: 
   Code: [Object],
    Symbol: [Object],
    BSON: [Object],
    DBRef: [Object],
    Binary: [Object],
    ObjectID: [Object],
    Long: [Object],
    Timestamp: [Object],
    Double: [Object],
    MinKey: [Object],
    MaxKey: [Object],
    promoteLongs: true ,
 _state: 'connected',
 pkFactory: 
   [Function: ObjectID]
    index: 14533369,
    createPk: [Function: createPk],
    createFromTime: [Function: createFromTime],
    createFromHexString: [Function: createFromHexString],
    isValid: [Function: isValid],
    ObjectID: [Circular],
    ObjectId: [Circular] ,
 forceServerObjectId: false,
 safe: false,
 notReplied: ,
 isInitializing: true,
 openCalled: true,
 commands: [],
 logger:  error: [Function], log: [Function], debug: [Function] ,
 tag: 1426645764322,
 eventHandlers: 
   error: [],
    parseError: [],
    poolReady: [],
    message: [],
    close: [] ,
 serializeFunctions: false,
 raw: false,
 recordQueryStats: false,
 retryMiliSeconds: 1000,
 numberOfRetries: 60,
 readPreference:  _type: 'ReadPreference', mode: 'primary', tags:     undefined  ,
collection: 
 db: 
   domain: null,
    _events: ,
    _maxListeners: 10,
    databaseName: 'askr',
    serverConfig: [Object],
    options: [Object],
    _applicationClosed: false,
    slaveOk: false,
    bufferMaxEntries: -1,
    native_parser: true,
    bsonLib: [Object],
    bson: [Object],
    bson_deserializer: [Object],
    bson_serializer: [Object],
    _state: 'connected',
    pkFactory: [Object],
    forceServerObjectId: false,
    safe: false,
    notReplied: ,
    isInitializing: true,
    openCalled: true,
    commands: [],
    logger: [Object],
    tag: 1426645764322,
    eventHandlers: [Object],
    serializeFunctions: false,
    raw: false,
    recordQueryStats: false,
    retryMiliSeconds: 1000,
    numberOfRetries: 60,
    readPreference: [Object] ,
 collectionName: 'users',
 internalHint: null,
 opts: ,
 slaveOk: false,
 serializeFunctions: false,
 raw: false,
 readPreference:  _type: 'ReadPreference', mode: 'primary', tags: undefined ,
 pkFactory: 
   [Function: ObjectID]
    index: 14533369,
    createPk: [Function: createPk],
    createFromTime: [Function: createFromTime],
    createFromHexString: [Function: createFromHexString],
    isValid: [Function: isValid],
    ObjectID: [Circular],
    ObjectId: [Circular] ,
 serverCapabilities: undefined ,
 selector: ,
 fields: undefined,
 skipValue: 0,
 limitValue: 0,
 sortValue: undefined,
 hint: null,
 explainValue: undefined,
 snapshot: undefined,
 timeout: true,
 tailable: undefined,
 awaitdata: undefined,
 oplogReplay: undefined,
 numberOfRetries: 5,
 currentNumberOfRetries: 5,
 batchSizeValue: 0,
 raw: false,
 readPreference:  _type: 'ReadPreference', mode: 'primary', tags: undefined ,
 returnKey: undefined,
 maxScan: undefined,
 min: undefined,
 max: undefined,
 showDiskLoc: undefined,
 comment: undefined,
 tailableRetryInterval: 100,
 exhaust: false,
 partial: false,
 slaveOk: false,
 maxTimeMSValue: undefined,
 connection: undefined,
 transforms: undefined,
 totalNumberOfRecords: 0,
 items: [],
 cursorId:  _bsontype: 'Long', low_: 0, high_: 0 ,
 dbName: undefined,
 state: 0,
 queryRun: false,
 getMoreTimer: false,
 collectionName: 'askr.users' 

【问题讨论】:

【参考方案1】:

它返回this,即collection 变量返回。这样您就可以进行链式调用:

Person
.find( occupation: /host/ )
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
.limit(10)
.sort('-occupation')
.select('name occupation')
.exec(callback);

查看示例here。

【讨论】:

这是用moongoose。我正在尝试使用 Mondodb 客户端。【参考方案2】:

db.collection.find 返回一个 Cursor 是 指向查询结果集的指针。客户端可以遍历游标来检索结果。默认情况下,光标在 10 分钟不活动后超时

参考:http://docs.mongodb.org/manual/reference/method/db.collection.find

如果你想存储 find() 的结果,你可以遍历它。 例如。

collection= db.collection('users');
result =collection.find().forEach(function(item)
//here item is record. ie. what you have to do with each record.
)

【讨论】:

需要将.forEach改为.each @vishalpatel 你这样吗?我指的是来自 mongo 的 cursor.forEach 方法;看这里:docs.mongodb.org/manual/reference/method/cursor.forEach 但是我得到了像“Object # has no method 'forEach'”这样的异常。当我用 .each 更改 forEach 时没有问题。 在这种情况下,我想知道您在节点上使用的 npm 模块来连接到 mongoDB。因为 Object.prototype.forEach 也是标准的 javascript 函数。 var MongoClient = require('mongodb').MongoClient;【参考方案3】:

你可以简单地使用:

collection.find().toArray();

【讨论】:

太棒了,帮我解决了。干杯

以上是关于mongodb ? db.collection.find() ???????的主要内容,如果未能解决你的问题,请参考以下文章

mongodb设置有权启动

win7 安装mongodb 怎么创建mongodb.log

MongoDB——MongoDB安装+增删改查操作

MongoDB——MongoDB安装+增删改查操作

window怎么安装mongodb

java怎么导出mongodb数据