简体   繁体   English

MongoDB:在索引n处查询“true”值的数组

[英]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.

相关问题 数组索引NO +值 - Array Index N.O. + Value Javascript 数组:如果数组连续包含 n 次值,则返回 true - Javascript Array: return true, if array contains value n times successively 带有数组索引值的MongoDB聚合推送 - Mongodb aggregate push with index value of array 如果查询值或查询值的一部分在数组中,则返回 true - If the query value, or part of the query value, is in the array, return true MongoDB javascript查询; 筛选列数组中的值 - MongoDB javascript query; filter for value in in a column array MongoDB查询以对存在特定数组索引的文档进行计数。 索引在javascript变量中 - MongoDB Query to count documents a where specific array index exists. Index is in javascript variable 如何使用布尔值查询 MongoDB 中的字段并返回一个布尔值是真还是假 - How to query a field in MongoDB with a boolean value and return a boolean whether it's true or false 为什么我的Mongoose查询没有更新MongoDB数据库中嵌套数组中的值? - Why is my Mongoose query not updating a value in a nested array in the MongoDB databse? 这在JavaScript中有什么作用:array [n] = true; - What does this do in JavaScript: array[n] = true; 更新所有文档,设置值等于数组的值,其中索引取自另一个值(MongoDB) - Update all documents setting a value equal to value of an array where index taken from another value (MongoDB)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM