studio 3T
更新或插入字段:
db.getCollection("data_379129").update({},{$set: {'connectedStatus':NumberInt(0),'connectedTime':null}},{multi:true})
- 字段类型判断
db.tb_name.find({"status":{$type:"double"}).count() //所有的status字段类型为Double类型的
db.tb_name.find({"status":{$type:1}).count() //所有status字段类型为Double类型的
以上两种方式均可以表示筛选Double类型的,在MongoDB中所有的字段值均为BSON类型实例,由于MongoDB的字段类型约束灵活,可以通过类型符号或别名进行筛选处理。
Type | Number | Alias | Notes |
---|---|---|---|
Double | 1 | “double” | |
String | 2 | “string” | |
Object | 3 | “object” | |
Array | 4 | “array” | |
Binary data | 5 | “binData” | |
Undefined | 6 | “undefined” | Deprecated. |
ObjectId | 7 | “objectId” | |
Boolean | 8 | “bool” | |
Date | 9 | “date” | |
Null | 10 | “null” | |
Regular Expression | 11 | “regex” | |
DBPointer | 12 | “dbPointer” | Deprecated. |
JavaScript | 13 | “javascript” | |
Symbol | 14 | “symbol” | Deprecated. |
JavaScript (with scope) | 15 | “javascriptWithScope” | |
32-bit integer | 16 | “int” | |
Timestamp | 17 | “timestamp” | |
64-bit integer | 18 | “long” | |
Decimal128 | 19 | “decimal” New in version 3.4. | |
Min key | -1 | “minKey” | |
Max key | 127 | “maxKey” |
- 字段类型处理
场景:将某个状态值进行位运算($bit)
修改订单状态,此时发现会偶发报错"Cannot apply $bit to a value of non-integral type._id: ObjectId('5987f81ba693c552e3eaa088') has the field status of non-integer type double"
,即正常应该为int,但在shell控制台设置int类型的数字时应使用NumberInt(10),否则会被作为Double类型存储
- 数据类型批量转换:
db.tb_name.find({"status":{$type:1}}).forEach(function(x){x.status=NumberInt(x.status);db.tb_name.save(x)})
- $bit 位运算的使用
db.tb_name.update({"_id" : ObjectId("5987f81ba693c552e3eaa088")},{$bit:{"status":{or:NumberInt(19)}}})
4、
字段类型判断语法
db.tb_name.find({"status":{$type:"double"}).count() //所有的status字段类型为Double类型的
db.tb_name.find({"status":{$type:1}).count() //所有status字段类型为Double类型的
5、数据类型批量转换
(double转为int32):
db.tb_name.find({"status":{$type:1}}).forEach(function(x){x.status=NumberInt(x.status);db.tb_name.save(x)})
(string转为array):
db.log.find({"record":{$type:2}}).forEach(function(x){x.record=Array(x.record);db.log.save(x)})标签:status,name,type,db,指令,mangoDB,类型,操作,tb From: https://www.cnblogs.com/yelanggu/p/16790721.html