[英]MongoDB: query Array for 'true' value at index n
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? 现在,我怎么可以查询这个集合来找到具有所有项目
true
用于指定工作日?
My first attempt was to use something like this: 我的第一次尝试是使用这样的东西:
// $elemMatch... // $ elemMatch ......
'weekdays[0]': true // Monday
But it did not seem to work. 但它似乎没有用。 I'm out of ideas atm and would appreciate any help!
我没有想法,欢迎任何帮助!
you can use this code 你可以使用这段代码
db.collectin.find({'weekdays.0' : true})
and if you want check 2 days: 如果你想检查2天:
db.collectin.find({$and :
[
{'weekdays.0' : true},
{'weekdays.1' : true}
]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.