【问题标题】:MongoDb find query giving weird responseMongoDb find 查询给出奇怪的响应
【发布时间】:2017-03-02 16:33:57
【问题描述】:

我正在尝试使用 nodejs 在 mongodb 中搜索数据。这是我的查询

collection.find({ age: { '$gt': 20 } });

它在 robomongo 中运行良好,但在我的应用程序中给了我这个响应

Readable {
 pool: null,
 server: null,
 disconnectHandler: 
  { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
   length: [Getter] },
  bson: {},
  ns: 'versioncontrol.Branch/contacts',
 cmd: 
  { find: 'versioncontrol.Branch/contacts',
    limit: 0,
 skip: 0,
  query: { age: [Object] },
 slaveOk: true,
 readPreference: { preference: 'primary', tags: undefined, options: [Object] } }

现在我不知道如何从中获取我的数据。

【问题讨论】:

    标签: javascript node.js mongodb robo3t


    【解决方案1】:

    find 方法返回的游标是可读流。您已经从中读取项目以获得实际结果。看this

    例子:

    var cursor = collection.find({ age: { '$gt': 20 } });
    cursor.each(function (err, doc) {
      if (err) {
        console.log(err);
      } else {
        console.log('Fetched:', doc);
      }
    });
    

    【讨论】:

    • 我应该如何在 testCase 中运行它?它不会导致异步问题吗?
    • 这能回答你的问题吗?
    • 我是这样做的。 var cursor = collection.find({ age: { '$gt': 20 } }).toArray();因为我想把所有的记录放在一起
    【解决方案2】:

    使用

    var cursor = collection.find({ age: { '$gt': 20 } }).toArray();
    cursor.then(function (docs) {
     console.log( docs ); 
    });
    

    【讨论】:

      猜你喜欢
      • 2016-08-29
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多