简体   繁体   中英

how can I construct my mongoose query so that at least one of two statements is always taken under consideration?

In my application user can add entry that is visible

This is my mongoose query:

Mongoose: test.find({updated_at: { '$gte': new Date("Fri, 14 Oct 2011 00:00:00 GMT"), '$lte': new Date("Fri, 14 Oct 2016 20:12:14 GMT") }, username: { '$in': [ 'XXXXXXXXX', 'YYYYYYYYYYYYYY' ] }, '$or': [ { myFlag: false } ] }) { fields: undefined }

and this is how I construct it:

    if (friends != undefined) {
            var friendsSplitted = friends.split(",");
            query = query.where("username").in(friendsSplitted);
       }


    if (publicEntries != undefined && publicEntries === "true") {
        query = query.or({myFlag: false});
    }

This query basically says:

look for all content that belongs to your friends OR for any content that has myFlag set up to false.

I want to change it so that is says:

look for all content that belongs to your friends AND ALSO for any content that has myFlag set up to false (BUT NOT necessarily belongs to your friends).

Can you help me with that?

You should try both queries inside $or as below:

test.find({'$or': [ { myFlag: false },
 {updated_at: { '$gte': new Date("Fri, 14 Oct 2011 00:00:00 GMT"), 
'$lte': new Date("Fri, 14 Oct 2016 20:12:14 GMT") }, 
username: { '$in': [ 'XXXXXXXXX', 'YYYYYYYYYYYYYY' ] } ] })

Hope this helps!

comment me if i missed something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM