Get all results from array that matches property

Viewed 2014

I know how to make MongoDB find a row based on an array like this:

useritems.find({userid: useridhere,  "items.id": idhere})

But how would I for example search and get all items that are activated, or get all items based on an items property? Like for example:

useritems.find({userid: useridhere,  "items.activated": true})

Would results in getting all items from the user where activated is true.

Here is my items schema:

var userItemsSchema = new Schema({
    userid : String,
    items: [
        {
            id: {type: Number, default: 1},
            activated: { type: Boolean, default: false},
            endtime : {type: Number, default: 0},
        },
    ],
});

module.exports = mongoose.model('useritems', userItemsSchema);
3 Answers
Related