Why my collection.find() does not work in meteor but work in robomongo?

泪湿孤枕 提交于 2020-01-11 12:36:13

问题


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

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.

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


回答1:


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.


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


来源:https://stackoverflow.com/questions/30787347/why-my-collection-find-does-not-work-in-meteor-but-work-in-robomongo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!