$bit
On this page本页内容
Definition定义
$bit
-
The$bit
operator performs a bitwise update of a field.$bit
运算符执行字段的逐位更新。The operator supports bitwise运算符支持按位and
, bitwiseor
, and bitwisexor
(i.e. exclusive or) operations.and
、按位or
和按位xor
(即exclusive或)操作。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>
,请使用点表示法。NoteAll numbers inmongosh
are doubles, not integers.mongosh
中的所有数字都是双数,而不是整数。To to specify integers in要在mongosh
, use theNumberInt()
or theNumberLong()
constructor.mongosh
中指定整数,请使用NumberInt()
或NumberLong()
构造函数。To learn more, see Int32 or Long.要了解更多信息,请参阅Int32或Long。To learn how your MongoDB driver handles numeric values, refer to your driver's documentation.要了解MongoDB驱动程序如何处理数值,请参阅驱动程序文档。
Behavior行为
Starting in MongoDB 5.0, 从MongoDB 5.0开始,当您将mongod
no longer raises an error when you use an update operator like $bit
with an empty operand expression ( { }
). $bit
之类的更新运算符与空操作数表达式({ }
)一起使用时,mongod
不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
Examples实例
The following examples use the 以下示例使用switches
collection:switchs
集合:
db.switches.insertMany( [
{ _id: 1, expdata: Int32(13) },
{ _id: 2, expdata: Int32(3) },
{ _id: 3, expdata: Int32(1) }
] )
Bitwise AND按位与
Use a bitwise 在and
in the updateOne()
operation to update expdata
.updateOne()
操作中使用按位and
来更新expdata
。
db.switches.updateOne(
{ _id: 1 },
{ $bit: { expdata: { and: Int32( 10 ) } } }
)
The bitwise 按位and
operation:and
操作:
gets the bitwise value of获取expdata
expdata
的按位值uses使用and
to apply the bitwise value of Int32(10)and
应用Int32(10)的逐位值updates使用结果更新expdata
with the result, 1000expdata
,1000
1101 // expdata
1010 // Int32(10)
----
1000
Binary 1000 is equivalent to Int32(8). 二进制1000相当于Int32(8)。The db.switches.find( { _id: 1 } )
command returns the following document:db.switches.find( { _id: 1 } )
命令返回以下文档:
{ "_id" : 1, "expdata" : 8 }
Bitwise OR按位或
Use a bitwise 在or
in the updateOne()
operation to update expdata
.updateOne()
操作中使用按位或来更新expdata
。
db.switches.updateOne(
{ _id: 2 },
{ $bit: { expdata: { or: Int32( 5 ) } } }
)
The bitwise 位or
operation:or
运算:
gets the bitwise value of获取expdata
expdata
的按位值uses使用or
to apply the bitwise value of Int32(5)or
应用Int32(5)的按位值updates使用结果更新expdata
with the result, 0111expdata
,0111
0111 // expdata
0101 // Int32(5)
----
0111
Binary 0111 is equivalent to Int32(7). 二进制0111相当于Int32(7)。The db.switches.find( { _id: 2 } )
command returns the following document:db.switches.find( { _id: 2 } )
命令返回以下文档:
{ "_id" : 2, "expdata" : 7 }
Bitwise XOR按位异或
Use a bitwise 在xor
in the updateOne()
operation to update expdata
.updateOne()
操作中使用按位xor
来更新expdata
。
db.switches.updateOne(
{ _id: 3 },
{ $bit: { expdata: { xor: Int32( 5 ) } } }
)
The bitwise 按位and
operation:and
操作:
gets the bitwise value of获取expdata
expdata
的按位值uses使用and
to apply the bitwise value of Int32(5)and
应用Int32(5)的逐位值updates使用结果更新expdata
with the result, 0100expdata
,0100
0001 // expdata
0101 // Int32(5)
----
0100
Binary 0100 is equivalent to 二进制0100相当于Int32(4)
. Int32(4)
。The db.switches.find( { _id: 3 } )
command returns the following document:db.switches.find( { _id: 3 } )
命令返回以下文档:
{ "_id" : 1, "expdata" : 4 }