H
H
HAbRAhabp2016-09-25 13:26:30
MongoDB
HAbRAhabp, 2016-09-25 13:26:30

Sampling from an array with schema in Mongoose?

I have a view schema in Mongoose:

var schema = new Schema({
// ...
views: [new Schema({
        userId: String,
        expired: Date
    })]
});
exports.Order = mongoose.model("Order", schema);

viewsis the subschema. I need to select rows from the main schema that EITHER do not contain views with that userId OR do, but views.expiredmust expire. Something similar to a system for blocking certain users with a period. How I try to make a selection:
Order.find({
        $and: [
            // other conditions
            { $or: [{"views.userId": { $ne: userId } }, { $and: [
                { "views.userId": userId },
                { "views.expired": { $gte: new Date()}}
            ]}]}
        ]
    }, function (err, data) {
        if (err) console.log(err);
        console.log(data);
    })

But this condition does not work for me.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question