3

So I have this collection containing a field "weekdays" representing weekdays Monday-Sunday (checked or not checked):

  var collection = [

    // work week (Monday to Friday)
    {
      weekdays: [true, true, true, true, true, false, false]
    },

    // weekend
    {
      weekdays: [false, false, false, false, false, true, true]
    },

    // ...

  ];

Now, how can I query this collection to find all items that has true for a specified weekday?

My first attempt was to use something like this:

// $elemMatch...

'weekdays[0]': true // Monday

But it did not seem to work. I'm out of ideas atm and would appreciate any help!

2
  • Don't claim to ask about 'truthy' while actually asking about 'true' - they're different things! Commented Jan 7, 2016 at 7:20
  • 1
    @ErwinWessels yeah, my humble apology. Commented Jan 7, 2016 at 15:39

1 Answer 1

2

you can use this code

db.collectin.find({'weekdays.0' : true})

and if you want check 2 days:

db.collectin.find({$and : 
    [
        {'weekdays.0' : true},
        {'weekdays.1' : true}
    ]
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.