loken
loken

Reputation: 305

Meteor Mongo collection multiple queries

I'm wondering if this is possible to do with Mongo/Meteor:

I've a collection, and documents all included a createdAt field and a status field (true/false boolean). I want to show users this collection in a list but only with documents that match:

if status is false AND if status is true only those before 14 days ago.

I can filter the list based on the two fields the way I want separately in my template helper, but how do I combine both restrictions together into one collection on the client side?


Update: so I think I am looking for an or statement as Patrick mentioned however it doesn't seem to work.

var date = new Date(+new Date - 12096e5);
    return Things.find({$and: [
        { $or : [ { status : false } ] },
        { $or : [ { status : true } , { createdAt: {"$gte": date} } ] }
    ]},
        {sort:{createdAt:-1}});

Doh, I have and and or backwards! I want items that match either of the two and conditions to be returned back and the below seems to work.

var date = new Date(+new Date - 12096e5);
    return Things.find({$or: [
        { $and : [ { status : false } ] },
        { $and : [ { status : true } , { createdAt: {"$gte": date} } ] }
    ]},
        {sort:{createdAt:-1}});

Upvotes: 0

Views: 694

Answers (1)

loken
loken

Reputation: 305

As Patrick mentioned I wanted and or statement, with an and to combine the set of two requirements, the below code works. Improved with help of David

var date = new Date(+new Date - 12096e5);
    return Things.find({$or: [
        { status : false },
        { status : true , createdAt: {"$gte": date} }
    ]},
        {sort:{createdAt:-1}});

Upvotes: 2

Related Questions