简体   繁体   English

流星Mongo查询集合中数组的子集

[英]Meteor Mongo query for subset of array in collection

I've run into a problem when querying for some stuff. 查询某些东西时遇到了问题。

I have a collection called Guestlists containing guestlists. 我有一个名为Guestlists的集合,其中包含来宾列表。 Each element contains a date and some other stuff together with an array called "list". 每个元素都包含一个日期和一些其他内容以及一个称为“列表”的数组。 The "list" array contains elements of each guestlist entry. “列表”数组包含每个来宾列表项的元素。 Every guestlist entry has an "entryOwner" meaning the person who added the entry to the list. 每个来宾列表条目都有一个“ entryOwner”,即将条目添加到列表中的人。

Here is the schema: 这是模式:

Schemas.Guestlist = new SimpleSchema({
  _id: { type: String, regEx: SimpleSchema.RegEx.Id },
  date: { type: Number },
  promoters: { type: [String], regEx: SimpleSchema.RegEx.Id, defaultValue: [] },
  list: { type: Array, defaultValue: [] },
  'list.$': { type: Object },
  'list.$.name': { type: String },
  'list.$.entryOwner': { type: String },
  'list.$.comment': { type: String, optional: true },
  'list.$.accepted': { type: Boolean, defaultValue: false },
  'list.$.rejected': { type: Boolean, defaultValue: false },
})

Now comes the problem. 现在出现了问题。 I want to query the database and get 1 guestlist and only the guestlist entries for a specific user-ID. 我想查询数据库并获取1个访客列表,并且仅获得特定用户ID的访客列表条目。

This is what I tried: 这是我尝试的:

Guestlists.find({_id : guestlistID, list: {$elemMatch: {entryOwner: user._id}}}, {fields: { 'list.$': 1}})

However, that only returns the guestlist with one item in the 'list' array, which is the first one. 但是,这只会返回带有“列表”数组中一项的客人列表,即第一项。 How can I get ALL the items in 'list' that has the entryOwner I query for? 如何获取“ list”中具有我要查询的entryOwner的所有项目?

Also tried this, but gives same result: 也尝试过此方法,但结果相同:

Guestlists.find({_id: guestlistID}, {fields: {list: {$elemMatch: {entryOwner: user._id}}}})

It is also important ta keep it as a cursor since I want to return this in my publications 将其保留为光标也很重要,因为我想在出版物中将其返回

Is this what you're looking for? 这是您要找的东西吗?

 Guestlists.findOne({{_id : guestlistID, list: {$elemMatch: {entryOwner: user._id}}}};

This limits the returned amount of GuestLists to one. 这会将GuestLists的返回数量限制为一个。 It is returned as an object and not as an array of length 1. 它作为对象而不是长度为1的数组返回。

EDIT 2016-03-08 编辑2016-03-08

Found a solution of how to solve the whole problem! 找到了如何解决整个问题的解决方案!

Now I use a package ( https://atmospherejs.com/jcbernack/reactive-aggregate ) and do exactly as they describe. 现在,我使用一个程序包( https://atmospherejs.com/jcbernack/reactive-aggregate )并按照它们的描述进行操作。

In the publication I do my aggregate and then set a client-side collection which to push the data to. 在出版物中,我进行汇总,然后设置将数据推送到的客户端集合。 Then I subscribe and fetch the data from the client-side collection. 然后,我订阅并从客户端集合中获取数据。


Okey, I got some help from the Meteor forums with this. Okey,我在Meteor论坛上获得了一些帮助。

What I was looking for was to use Aggregate in MongoDB! 我正在寻找的是在MongoDB中使用Aggregate! It does exist a nice package for this: https://github.com/meteorhacks/meteor-aggregate 它确实存在一个很好的软件包: https : //github.com/meteorhacks/meteor-aggregate

This is how my code looks now: 这是我的代码现在的样子:

guestList = Guestlists.aggregate([
    { $match: {nightclubId: nightclubId, date: dateTime}},
    { $unwind: '$list'},
    { $match: {'list.entryOwner': user._id}},
    { $group: {_id: '$_id', list: {$push: '$list'}}}
  ])

Now my problem is how to return this from my publication to the client. 现在,我的问题是如何将其从出版物中退还给客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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