2017-10-12 219 views
1

我有一個貓鼬模式,模擬社交網絡中的用戶。 其中我想保存一個用戶擁有的所有聊天數組,一個聊天包含一個分數(用戶正在與之聊天的用戶)和一個代表對話的數組。嵌套mongodb查詢

var userSchema = mongoose.Schema({ 
    email  : String, 
    chats   : [{ respondent : String, conversation : [{ message: String, author : String}]}], 
}); 

如何查找包含特定受訪者的聊天內容?在找到的項目中,id喜歡將消息推送到對話。

回答

1

你沒有提及任何內部聊天的關鍵。指定這樣的:

var userSchema = mongoose.Schema({ 
     email  : String, 
     chats   : [ 
     { respondent : String, 
      innerChat:[{ message: String, author : String}]} 
     ], 
    }); 

查詢添加文檔解決方案是:

var document={ 
email:"[email protected]", 
chats:[{respondent:"UserName",innerChat:[{message:"hello",author:"authorName"}]}] 
} 
db.collection.insert(document); 

查詢對內部查詢查找文檔

db.collection.find({"chats.innerChat.author":"authorName"}); 
+0

感謝我忘了把鑰匙,我的問題是,我需要用被訪者找到innerChat(或者我稱之爲對話)。 –

+0

你提到你想推動物品在內部聊天。請更新你的問題 – Sam

+0

好吧,爲了公平我說:「使用受訪者,我如何推物品到內部聊天」,但我看到你在哪裏來,問題已更新。 –