2016-02-25 60 views
0

我有信使平臺在我的應用程序,我在下面的格式獲得一個價值獨特的陣列值的記錄存在

{ 
    userIds: [ 'user1Id', 'user2Id' ], 
    msg: "message", 
    sentBy: 'user1', 
    sentAt: new Date() 
} 


{ 
    userIds: [ 'user1Id', 'user3Id' ], 
    msg: "message", 
    sentBy: 'user1', 
    sentAt: new Date() 
} 


{ 
    userIds: [ 'user1Id', 'user2Id' ], 
    msg: "message", 
    sentBy: 'user1', 
    sentAt: new Date() 
} 

節省每封郵件有沒有什麼辦法讓從收集獨特的對話?

例如從上面所列內容我想

{ 
     userIds: [ 'user1Id', 'user2Id' ], 
     msg: "message", 
     sentBy: 'user1', 
     sentAt: new Date() 
    } 


    { 
     userIds: [ 'user1Id', 'user3Id' ], 
     msg: "message", 
     sentBy: 'user1', 
     sentAt: new Date() 
    } 

這兩個記錄,

什麼都可以在這裏蒙戈查詢?

除查詢所有記錄以外是否還有其他方法可以手動進行獨特操作?

或任何人建議更好的架構,我剛開始使用此功能,所以我可以改變架構。

我考慮使用像

{ 
    userIds: [ 'user1Id', 'user2Id' ], 
    msgs: [ 
    { 
     msg: "message", 
     sentBy: 'user1', 
     sentAt: new Date() 
    }, 
    { 
     msg: "message", 
     sentBy: 'user2', 
     sentAt: new Date() 
    } 
    ], 
    modifiedAt: new Date() 
} 

而是反對決定,因爲只要有加入的MSG數組新的味精整場將因此在使用第一個模式被髮送到客戶端。

任何建議appreaciated。謝謝。

回答

1

你可以使用聚合做到這一點,

.aggregate([{$組:{_ ID: 「$用戶id」}}])

參考this更多細節

你可以使用$ project選項將返回文檔作爲您希望的格式。

+0

@ user555請參閱此鏈接https://github.com/meteorhacks/meteor-aggregate –

0

我建議你簡單地使用聚合。例如:

db.test.aggregate({$group: {_id: "$userIds" } }) 

將返回

{ 
    "result" : [ 
     { 
      "_id" : [ 
       "user1Id", 
       "user3Id" 
      ] 
     }, 
     { 
      "_id" : [ 
       "user1Id", 
       "user2Id" 
      ] 
     } 
    ], 
    "ok" : 1 
} 

您將在文檔Aggregation-Distinct瞭解詳情。