2014-10-18 135 views
0

我要做到以下幾點,可以說一個簡單的羣聊應用程序,我們有模型,如Sails.js水線查詢

消息

  • 房間:{「模式」:房}

  • 用戶:{ '收集':用戶}

我想要以下內容:

給定一個用戶Bob,獲取包含Bob的房間中的所有消息。

這是什麼適當的水線查詢?

回答

0

我不知道我知道你的意思是你的模型結構,但我猜你需要的東西是這樣的:

Message.find({ //Message is your Message model 
    room:room_id, //or some other identifier 
    content:{contains:'Bob'} 
}).exec(function(error,result){ 
    if(error){ 
    // handle error here 
    }else{ 
    var messages = result; 
    //do your stuff. 
    } 
}) 

如果我錯了,你的意思,請讓我知道: )

0

我建議在消息模型中添加用戶(Bob's)ID和房間ID作爲字段。例如,這可能包括在MessageModule.js:

userId : 'string', 
roomId : 'string', 

你的查詢來獲取所有Bob的一間客房的消息是如此簡單:

Messages.find({userId: bobs_user_id, roomId: room_id}, 
function(err, messages){ 
    // all bob's messages from room room_id are available in messages 
});