On this page本页内容
$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
运算符表达式,请使用以下原型:
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.有关更多信息,请参阅NumberInt或NumberLong。
Consider the following document inserted into the collection 考虑插入集合switches
:switches
中的以下文档:
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
运算的结果:
The bitwise 按位and
operation results in the integer 8 (i.e. 1000
):and
运算的结果是整数8
(即1000
):
And the updated document has the following value for 更新后的文档对expdata
:expdata
具有以下值:
The mongo
shell displays NumberInt(8)
as 8
.mongo
shell将NumberInt(8)
显示为8。
Consider the following document inserted into the collection 考虑插入集合switches
:switches
中的以下文档:
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
运算的结果:
The bitwise 按位or
operation results in the integer 7 (i.e. 0111
):or
运算产生整数7(即0111
):
And the updated document has the following value for 更新后的文档对expdata
:expdata
具有以下值:
Consider the following document in the collection 在集合switches
:switches
中考虑以下文档:
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
运算的结果:
The bitwise 按位xor
operation results in the integer 4:xor
运算产生整数4:
And the updated document has the following value for 更新后的文档对expdata
:expdata
具有以下值:
See also参阅