2016-03-07 88 views
0

我在查询某些内容时遇到了问题。Meteor Mongo查询集合中数组的子集

我有一个名为Guestlists的包含guestlist的集合。每个元素包含一个日期和其他一些东西以及一个名为“list”的数组。 “list”数组包含每个访客列表条目的元素。每个访客列表条目都有一个“entryOwner”,意思是将条目添加到列表中的人员。

这里是架构:

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

现在问题来了。我想查询数据库,并获得1个访客列表,只有特定用户ID的访客列表条目。

这是我的尝试:

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

但是,只有一个项目返回guestlist的“清单”阵列,这是第1英寸我如何获得具有我查询的entryOwner的'list'中的所有项目?

也试过这个,但给同样的结果:

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

同样重要的是TA保持它作为一个游标,因为我想在我的出版物

回答

0

返回这是这一点,你在找什么对于?

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

这将限制返回的GuestLists数量为1。它被返回作为对象,而不是作为长度1.

+0

不,这不是我所期待的,找到它。很快就会发布! –

0

EDIT 2016年3月8日

的阵列实测值如何解决整个问题的解决方案!

现在我使用一个软件包(https://atmospherejs.com/jcbernack/reactive-aggregate)并按照他们的描述进行操作。

在出版物中,我做了我的聚合,然后设置将数据推送到的客户端集合。然后我订阅并从客户端收集数据。


Okey,我从流星论坛得到了一些帮助。

我在找的是在MongoDB中使用Aggregate!它确实存在一个漂亮的包这样的: https://github.com/meteorhacks/meteor-aggregate

这是我的代码现在的样子:

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

现在我的问题是如何从我的刊物返回给客户。