$bit

On this page本页内容

Definition定义

$bit

The $bit operator performs a bitwise update of a field. $bit运算符对字段执行按位更新。The operator supports bitwise and, bitwise or, and bitwise xor (i.e. exclusive or) operations. 运算符支持按位and、按位or和按位xor(即异或)运算。To specify a $bit operator expression, use the following prototype:要指定$bit运算符表达式,请使用以下原型:

{ $bit: { <field>: { <and|or|xor>: <int> } } }

Only use this operator with integer fields (either 32-bit integer or 64-bit integer).仅将此运算符用于整数字段(32位整数或64位整数)。

To specify a <field> in an embedded document or in an array, use dot notation.若要在嵌入式文档或数组中指定一个<field>,请使用点表示法

Note

All numbers in the mongo shell are doubles, not integers. mongo shell中的所有数字都是双精度的,而不是整型数。Use the NumberInt() or the NumberLong() constructor to specify integers. 请使用NumberRint()NumberLong()构造函数指定整数。See NumberInt or NumberLong for more information.有关更多信息,请参阅NumberIntNumberLong

Examples示例

Bitwise AND按位与

Consider the following document inserted into the collection switches:考虑插入集合switches中的以下文档:

{ _id: 1, expdata: NumberInt(13) }

The following update() operation updates the expdata field to the result of a bitwise and operation between the current value NumberInt(13) (i.e. 1101) and NumberInt(10) (i.e. 1010):下面的update()操作将expdata字段更新为当前值NumberInt(13)(即1101)和NumberInt(10)(即1010)之间按位and运算的结果:

db.switches.update(
   { _id: 1 },
   { $bit: { expdata: { and: NumberInt(10) } } }
)

The bitwise and operation results in the integer 8 (i.e. 1000):按位and运算的结果是整数8(即1000):

1101
1010
----
1000

And the updated document has the following value for expdata:更新后的文档对expdata具有以下值:

{ "_id" : 1, "expdata" : 8 }

The mongo shell displays NumberInt(8) as 8.mongo shell将NumberInt(8)显示为8。

Bitwise OR按位或

Consider the following document inserted into the collection switches:考虑插入集合switches中的以下文档:

{ _id: 2, expdata: NumberLong(3) }

The following update() operation updates the expdata field to the result of a bitwise or operation between the current value NumberLong(3) (i.e. 0011) and NumberInt(5) (i.e. 0101):以下update()操作将expdata字段更新为当前值NumberLong(3)(即0011)和NumberInt(5)(即0101)之间按位or运算的结果:

db.switches.update(
   { _id: 2 },
   { $bit: { expdata: { or: NumberInt(5) } } }
)

The bitwise or operation results in the integer 7 (i.e. 0111):按位or运算产生整数7(即0111):

0011
0101
----
0111

And the updated document has the following value for expdata:更新后的文档对expdata具有以下值:

{ "_id" : 2, "expdata" : NumberLong(7) }

Bitwise XOR按位异或

Consider the following document in the collection switches:在集合switches中考虑以下文档:

{ _id: 3, expdata: NumberLong(1) }

The following update() operation updates the expdata field to the result of a bitwise xor operation between the current value NumberLong(1) (i.e. 0001) and NumberInt(5) (i.e. 0101):下面的update()操作将expdata字段更新为当前值NumberLong(1)(即0001)和NumberInt(5)(即0101)之间按位xor运算的结果:

db.switches.update(
   { _id: 3 },
   { $bit: { expdata: { xor: NumberInt(5) } } }
)

The bitwise xor operation results in the integer 4:按位xor运算产生整数4:

0001
0101
----
0100

And the updated document has the following value for expdata:更新后的文档对expdata具有以下值:

{ "_id" : 3, "expdata" : NumberLong(4) }