MongoDB:查询数组以获取索引n处的"true"值 [英] MongoDB: query Array for 'true' value at index n

查看:77
本文介绍了MongoDB:查询数组以获取索引n处的"true"值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个包含工作日"字段的集合,该字段代表周一至周日的工作日(选中或未选中):

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]
    },

    // ...

  ];

现在,如何查询该集合以查找指定工作日内所有具有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 ...

// $elemMatch...

'weekdays[0]': true // Monday

但是它似乎没有用.我没有自动取款机的想法,将不胜感激!

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

推荐答案

您可以使用此代码

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

,如果要检查2天:

db.collectin.find({$and : 
    [
        {'weekdays.0' : true},
        {'weekdays.1' : true}
    ]
})

这篇关于MongoDB:查询数组以获取索引n处的"true"值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆