【问题标题】:Error showing when used fields in the mongoDB query在 mongoDB 查询中使用字段时显示错误
【发布时间】:2019-03-04 22:54:53
【问题描述】:

我正在尝试在 MeteorJS MongoDB 中查找特定文件,但它不起作用。我是这个环境的新手。

const gallery = Gallery.find( 
    { userID: Meteor.userId() }, 
    { fields: { _id: 1, projectImage: 1, projectVideo: 0 } }, 
    { sort: { createdAt: -1 }, 
    limit: Session.get("eventLimit") } ).fetch({});

【问题讨论】:

  • { fields: { _id: 1, projectImage: 1 }, sort: { .. }, limit: { } }。不在单独的对象中,也不要将01 混合使用。我确实给了你相关的文档链接,所以你真的不需要问这个。
  • 请提供更多关于究竟什么是“不工作”的信息。例如,写下预期的结果和你得到的结果。

标签: javascript mongodb meteor ecmascript-6


【解决方案1】:

你的语法是错误的,首先,“fields”、“limit”和“sort”应该都在 1 个“options”对象中,并且 fetch 不需要参数,这个例子有效:

const gallery = Gallery.find({
  userID: Meteor.userId()
},{ 
  fields: { _id: 1, projectImage: 1}, 
  sort: { createdAt: -1 }, 
  limit: Session.get("eventLimit") 
}).fetch();

另外,请注意我从示例中删除了“projectVideo:0”,“不能混合包含和排除样式:键必须全为 1 或全为 0。例外是您可以指定 _id : 0 在包含说明符中,这也会将 _id 排除在结果对象之外。” (来自 Meteor 文档 collections#fieldspecifiers

因此,您必须选择将您想要的字段用 1 列入白名单,或将不需要的字段用 0 列入黑名单。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 2018-12-05
    • 2021-05-21
    • 2016-01-24
    • 2018-03-27
    • 2019-01-23
    • 2013-08-24
    • 2021-08-07
    相关资源
    最近更新 更多