【问题标题】:Why my collection.find() does not work in meteor but work in robomongo?为什么我的 collection.find() 在流星中不起作用但在 robomongo 中起作用?
【发布时间】:2015-06-11 17:17:31
【问题描述】:

我有一个出版物,应该返回所有匹配 _id 数组的用户。这里是查询:

Meteor.users.find({
            '_id': { $in: myArray}},{
                'profile.name':1,
                'profile.description':1,
                'profile.picture':1,
                'profile.website':1,
                'profile.country':1}
            );

当我在 Robomongo(mongo 浏览器)中运行它时,它可以工作。但我的出版物只返回undefined。当我在出版物中console.log(myArray); 时,我得到类似['FZ78Pr82JPz66Gc3p'] 的内容。这是我粘贴在我的工作 Robomongo 查询中的内容。

替代问题:如何从Collection.find() 结果中获得更好的反馈(日志)?

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    您似乎正在尝试在 find 中指定字段,您可以这样做:

    var options = {
      fields: {
        'profile.name': 1,
        'profile.description': 1,
        'profile.picture': 1,
        'profile.website': 1,
        'profile.country': 1
      }
    };
    
    Meteor.users.find({_id: {$in: myArray}}, options);
    

    但是,如果在发布函数中使用它,我强烈建议只使用像这样的顶级字段:

    Meteor.users.find({_id: {$in: myArray}}, {fields: {profile: 1}});
    

    有关原因的更多详细信息,请参阅this question


    对于第二个问题,您可以通过调用fetch 来查看光标返回的文档。例如:

    console.log(Posts.find({_id: {$in: postIds}}).fetch());
    

    【讨论】:

    • 这是一个不错的答案。感谢fetch() 的把戏,我应该猜到了。我会阅读您的链接,尝试您的方法,然后回来验证/提供反馈。谢谢!
    • 它就像一个魅力。感谢您更正缺少的 fields 密钥。我又忘记了...... :'(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    相关资源
    最近更新 更多