猫鼬找到返回奇怪的对象 [英] Mongoose find returning odd object

查看:23
本文介绍了猫鼬找到返回奇怪的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的问题是 db.collection.find() mongoose 命令.我对 mongoose/mongodb 比较陌生,但我已经掌握了它的概念.这是我一直在尝试运行的测试代码:

my current problem is with the db.collection.find() mongoose command. I'm relatively new to mongoose/mongodb, but I've gotten the hang of the concepts of it. Here is the test code I've been trying to run:

mongoose.connect(url);
function main() 
{
    var db = mongoose.connection;
    db.on('open', function() {

        db.collection('Tweet').find({id: 631460910368956400}, function (err, data){
            console.log(data);
        })


        /*var coll = db.collection('Tweet');
         db.collection('Tweet').findOne({id: 631460910368956400},function (err, ret) {
            if(err) console.log(err);
            console.log(ret['id']);

         //db.close();
        });*/
    });
} 
main();

未注释掉的字段返回的数据是一个奇怪的对象:

The data returned from the non commented out field is a strange object:

{ connection: null,
  server: null,
  disconnectHandler:
   { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
     length: [Getter] },
  bson: {},
  ns: 'TEST.Tweet',
  cmd: { find: 'TEST.Tweet', limit: 0, skip: 0, query: {}, slaveOk: false },
  options:
   { skip: 0,
     limit: 0,
     raw: undefined,
     hint: null,
     timeout: undefined,
     slaveOk: false,
     db:
      { domain: null,
        _events: [Object],
        _maxListeners: undefined,
        s: [Object],
        serverConfig: [Getter],
         bufferMaxEntries: [Getter],
         databaseName: [Getter],

等等等等......它会持续更长时间.

etc etc... it goes on for much longer.

IP 地址是成功连接的远程连接.我可以执行诸如添加和删除文档之类的操作,但实际上无法从 javascript 中查看文档.我知道这是由于某种异步问题引起的,但是我不确定如何解决它.此外,.findOne() 的注释掉的代码似乎在上面的代码中完全正确地提取了数据.

The IP address is a remote connection that successfully connects. I can do things like add and remove documents, but cannot actually view the documents from the javascript. I know that it is caused due to some kind of asynchronous problem, however I'm not sure how to fix it. Also, the commented out code for .findOne() seems to pull data completely fine in the code above.

.find() 函数的代码有什么问题?对为什么当前数据检索错误的解释也很好.

What would be the problem with the code for the .find() function? An explanation for why the current error in data retrieving would be great also.

感谢您的帮助!

推荐答案

您收到的对象是一个 Cursor 这是一个用于检索实际结果的对象.

The object you receive is a Cursor which is an object used to retrieve the actual results.

当您确定您的查询永远不会返回多个对象时(例如在这种情况下,您通过始终唯一的 _id 字段进行查询),您可以使用 db.collection('Tweet').findOne( 它将只返回该对象,而没有额外的间接层.

When you are sure your query will never return more than one object (like in this case where you query by the always unique _id field), you can use db.collection('Tweet').findOne( which will return just that object without the additional layer of indirection.

但是当您的查询可能返回多个文档时,您需要使用游标.要解析游标,您可以使用 cursor.toArray 将其转换为文档数组:

But when your query can potentially return more than one document, you need to use a cursor. To resolve the cursor, you can turn it into an array of documents by using cursor.toArray:

    db.collection('Tweet').find({}, function (err, cursor){
        cursor.toArray().forEach(function(doc) { 
            console.log(doc);
        });
    })

这是最简单的版本.有关游标的更多信息,请参阅上面链接的文档.

This is the most simple version. For more information about cursors, refer to the documentation linked above.

顺便说一句:到目前为止,您只使用了本机驱动程序的功能.当您想使用 Mongoose 查询对象时,您可能需要使用 Mongoose 模型对象的方法.

By the way: So far you only used the functionality of the native driver. When you want to use Mongoose to query objects, you might want to use the methods of the Mongoose model object.

这篇关于猫鼬找到返回奇怪的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆