在蒙戈设置单独的比特,来存储位掩码 [英] Setting individual bits in mongo, to store a bitmask

查看:127
本文介绍了在蒙戈设置单独的比特,来存储位掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些标志存储到一个蒙戈分贝。现在我有以下几点:

I want to store some flags into a mongo db. For now I have the following:

> 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"})

有没有更有效的方法?

Is there a more effective way?

推荐答案

虽然你可以这样做的,我会建议使用每个标志独立的布尔字段。这将占用更多的空间,但会更快进行查询,因为它不会使用JavaScript,如果需要,可以使用索引。如果您需要为您的应用程序的其他部分的位域,你可以让他们既最新像这样(假设A,B,C ...地图位0,1,2 ...):

While you can do it that way, I'd suggest using separate boolean fields for each flag. That will take up more space but will be faster to query because it won't use javascript and can use indexes if needed. If you need the bitfield for other parts of your application you can keep them both up to date like this (assuming a,b,c... map to bits 0,1,2...):

db.c.update({_id:ID}, {$set:{a:true}, $bit:{bits: {or: 0x1}}})
db.c.update({_id:ID}, {$set:{c:false}, $bit:{bits: {and: ~0x8}}})

这篇关于在蒙戈设置单独的比特,来存储位掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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