Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 在mongo中设置单个位以存储位掩码_Mongodb_Bit Manipulation - Fatal编程技术网

Mongodb 在mongo中设置单个位以存储位掩码

Mongodb 在mongo中设置单个位以存储位掩码,mongodb,bit-manipulation,Mongodb,Bit Manipulation,我想将一些标志存储到mongo db中。目前,我有以下几点: > db.test.save({a:0x1}) > db.test.save({a:0x3}) > db.test.save({a:0x2}) > db.test.save({a:0x2}) > db.test.save({a:0x4}) > db.test.save({a:0x5}) > db.test.find({'$where': "this.a & 0x1"}) 有没有更有效

我想将一些标志存储到mongo db中。目前,我有以下几点:

> db.test.save({a:0x1})
> db.test.save({a:0x3})
> db.test.save({a:0x2})
> db.test.save({a:0x2})
> db.test.save({a:0x4})
> db.test.save({a:0x5})
> db.test.find({'$where': "this.a & 0x1"})

有没有更有效的方法?

虽然可以这样做,但我建议对每个标志使用单独的布尔字段。这将占用更多空间,但查询速度更快,因为它不使用javascript,并且可以在需要时使用索引。如果您的应用程序的其他部分需要位字段,您可以像这样使它们都保持最新(假设a、b、c…映射到位0、1、2…):

当您使用$where:“this.myField&0x1”时,它与$where:“0”和$where:“1”相同
这是错误的,因为0==false=true,但是0==false=false

谢谢。我当然知道这种可能性。但是没有办法像
db.test.find({a:{'$and':0x1}}})
db.test.find({a:{'$&':0x1}}})
那样进行查询吗?没有,但我们已经讨论过了。我找不到jira案例,所以如果你想要的话,我建议你在jira.mongodb.org开一张罚单,如果有任何变化,你都会得到通知,这将是非常棒的!非常感谢。此外,使用模运算符查询位字段不会使用索引,但正则表达式可以以任何方式使用索引吗?正则表达式只能将索引用于前缀查询。另外,我不认为模运算符会做你想做的,但如果可以,我想看看如何做。嗨,埃尔瑟!问题是设置位,而不是按位获取。它发生在每个人身上,你阅读问题并回答你阅读的内容。可悲的是,有时我们看不到真正的问题。我总是建议你在写完问题和答案后,重新阅读它们。以后您可以随时编辑您的答案。祝你好运
db.c.update({_id:ID}, {$set:{a:true}, $bit:{bits: {or: 0x1}}})
db.c.update({_id:ID}, {$set:{c:false}, $bit:{bits: {and: ~0x8}}})