2016-07-09 37 views
0

我有兩個集合RoomMessage如何限制從相關集合中獲取的字段?

我所試圖做的是獲取所有的房間,並在每個房間的最後一條消息簡單的聊天設置。所以,我想:

Room.find({ 
     where:{ 
      or:[{myId:id},{partnerId:id}] 
     }, 
     order:['updatedAt DESC'], 
     limit:10, 
     skip: (page-1)*10, 
     include:[ 
      { 
       relation:"messages", 
       scope:{ 
        where:{toId:id}, 
        limit:1, 
        order:['createdAt DESC'] 
       } 
      } 
     ]},function(err,rooms){}) 

以上只獲取一個在所有房間的消息,所以基本上我有一百個房間,只有一個在結果最新消息,而我想在每個房間的最後一條消息。這怎麼能實現?

+0

您的查詢正確。你確定你使用正確的ID嗎?我的意思是你的數據模型與這個查詢匹配? –

回答

0

我不知道你的數據模型。但我認爲你需要從include刪除where子句。

Room.find({ 
     where:{ 
      or:[{myId:id},{partnerId:id}] 
     }, 
     order:['updatedAt DESC'], 
     limit:10, 
     skip: (page-1)*10, 
     include:[ 
      { 
       relation:"messages", 
       scope:{ 
        limit:1, 
        order:['createdAt DESC'] 
       } 
      } 
     ]},function(err,rooms){}) 
相關問題