2016-03-07 95 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'}}} 
    ]) 

現在我的問題是如何從我的刊物返回給客戶。