簡體   English   中英

Mongoose 在對象數組中找到

[英]Mongoose find in array of objects

我有一個貓鼬模式

var MessageSchema = new Schema({
    streamer: {
        streamer_username: String,
        streams: [{
            id: String,
            messages: [{
                date: String,
                username: String,
                message: String,
                song: String
            }]
        }]
    }
})

如您所見,它包含一個數組“streams”,其中包含具有“id”值的對象。 我正在嘗試使用此查詢搜索數據庫。

MsgSchema.find({ "streamer.streamer_username" : streamer_name, "streamer.streams": { "$in": {id: response.data[0].id} }}, (err, found) =>{}})

但我什么也沒找到。 即使我刪除了第一個“用戶名”部分,它仍然沒有找到任何東西並返回一個空數組,所以問題顯然是第二部分。 我的查詢有什么問題? 我在文檔中找不到任何關於它的信息。

我遲到了,但我希望它可以幫助其他人

您可以簡單地使用.find()並在其中創建嵌套在文檔數組中的多個字段的條件規范。 最簡單的方法是:

MessageSchema.find({ "streamer.streams": { id: "XXX" } });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM