[英]Meteor Mongo query for subset of array in collection
查询某些东西时遇到了问题。
我有一个名为Guestlists的集合,其中包含来宾列表。 每个元素都包含一个日期和一些其他内容以及一个称为“列表”的数组。 “列表”数组包含每个来宾列表项的元素。 每个来宾列表条目都有一个“ 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}})
但是,这只会返回带有“列表”数组中一项的客人列表,即第一项。 如何获取“ list”中具有我要查询的entryOwner的所有项目?
也尝试过此方法,但结果相同:
Guestlists.find({_id: guestlistID}, {fields: {list: {$elemMatch: {entryOwner: user._id}}}})
将其保留为光标也很重要,因为我想在出版物中将其返回
这是您要找的东西吗?
Guestlists.findOne({{_id : guestlistID, list: {$elemMatch: {entryOwner: user._id}}}};
这会将GuestLists的返回数量限制为一个。 它作为对象而不是长度为1的数组返回。
编辑2016-03-08
找到了如何解决整个问题的解决方案!
现在,我使用一个程序包( https://atmospherejs.com/jcbernack/reactive-aggregate )并按照它们的描述进行操作。
在出版物中,我进行汇总,然后设置将数据推送到的客户端集合。 然后,我订阅并从客户端集合中获取数据。
Okey,我在Meteor论坛上获得了一些帮助。
我正在寻找的是在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'}}}
])
现在,我的问题是如何将其从出版物中退还给客户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.