mangoDB操作指令

studio 3T

  更新或插入字段:

  db.getCollection("data_379129").update({},{$set: {'connectedStatus':NumberInt(0),'connectedTime':null}},{multi:true})

  1. 字段類型判斷
    db.tb_name.find({"status":{$type:"double"}).count() //所有的status字段類型爲Double類型的
    db.tb_name.find({"status":{$type:1}).count() //所有status字段類型爲Double類型的
    以上兩種方式均可以表示篩選Double類型的,在MongoDB中所有的字段值均爲BSON類型實例,由於MongoDB的字段類型約束靈活,可以通過類型符號或別名進行篩選處理。
TypeNumberAliasNotes
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”  
  1. 字段類型處理
    場景:將某個狀態值進行位運算($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)})
  1. $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)})
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章