Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a Mongoose schema

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

It contains an array "streams", which contains objects that have the "id" value, as you can see. I'm trying to search the DB with this query.

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

But i find nothing. Even if i remove the first "username" part, it still doesn't find anything and returns an empty array, so the problem is clearly the second part. What is wrong with my query? I can't find anything about it in the documentation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
248 views
Welcome To Ask or Share your Answers For Others

1 Answer

I am late but I hope it helps other people

You can simply use .find() and inside it, you can create the specifications of the conditions on more than one field nested in an array of documents. The easiest way is:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...