简体   繁体   中英

Error showing when used fields in the mongoDB query

I am trying to find specific filed in the MeteorJS MongoDB but it's not working. I am new to this environment.

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

Your syntax is wrong, first, "fields", "limit" and "sort" should all be in 1 "options" object and fetch does not require an argument, this example works:

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

also, please note that I removed "projectVideo:0" from my example, "it is not possible to mix inclusion and exclusion styles: the keys must either be all 1 or all 0. The exception is that you may specify _id: 0 in an inclusion specifier, which will leave _id out of the result object as well." (from the Meteor docs collections#fieldspecifiers )

So you have to choose to whitelist your wanted fields with 1's or blacklist unwanted fields with 0.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM