本文介绍了为什么我的 collection.find() 在meteor 中不起作用但在robomongo 中起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a publication that should return me all users matching an _id array. here it the query:

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 查询中的内容.

When I run it in Robomongo (the mongo browser), it works. But my publication only returns undefined. when I console.log(myArray); in the publication, I get something like this ['FZ78Pr82JPz66Gc3p']. This is what I paste in my working Robomongo query.

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

Alternative question: how can I have a better feedback(log) from the Collection.find() result?

推荐答案

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

It looks like you are trying to specify fields in your find, which you can do like this:

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);

但是,如果这是在发布功能中使用,我强烈建议仅使用顶级字段,如下所示:

However, if this is being used in a publish function, I strongly recommend using only top-level fields like so:

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

有关原因的更多详细信息,请参阅这个问题.

For more details on why, please see this question.

对于您的第二个问题,您可以通过调用fetch 就可以了.例如:

For your second question, you can view the documents returned by a cursor by calling fetch on it. For example:

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

这篇关于为什么我的 collection.find() 在meteor 中不起作用但在robomongo 中起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:54