操作案例干貨合集
修改資料
db.getSiblingDB('cqust');
var 查詢條件={'sno':2019000000};
var 更新條件={$set:{'body.weight':100,'name':'王小王'}};
db.students.update(查詢條件, 更新條件);
如何去修改資料:首先我們要明確我們需要修改的資料處于哪一個位置,或者在哪一個欄位,我們利用$set:{欄位:新值},其實對于這個我們很明顯的就是修改陣列欄位,
其他操作
$type運算子是基于BSON型別來檢索集合中匹配的資料型別,并回傳結果,
利用欄位的型別進行查詢:$type
首先我們在查詢條件里面加入我們需要查詢的欄位名,然后就是一個:{$type:'string'},也可以數字:2
MongoDB 中可以使用的型別如下表所示
| 型別 | 數字 | 備注 |
|---|---|---|
| Double | 1 | |
| String | 2 | |
| Object | 3 | |
| Array | 4 | |
| Binary data | 5 | |
| Undefined | 6 | 已廢棄, |
| Object id | 7 | |
| Boolean | 8 | |
| Date | 9 | |
| Null | 10 | |
| Regular Expression | 11 | |
| JavaScript | 13 | |
| Symbol | 14 | |
| JavaScript (with scope) | 15 | |
| 32-bit integer | 16 | |
| Timestamp | 17 | |
| 64-bit integer | 18 | |
| Min key | 255 | Query with -1. |
| Max key | 127 |
db.getSiblingDB('cqust');
var 查詢條件={'body.weight':{$type:'string'}}; //2也可以
var 查詢條件={'body.weight':{$type:'double'}}; //1也可以
var 回傳條件={};
db.students.find(查詢條件,回傳條件)
這樣所查詢到的資料就會是我們所需要的資料型別資料
利用math.random進行亂數的生成,最后完成我們的更新操作
//修改2019000000的身高為 160-170以內的亂數
//修改2019000000的身高屬性改為字串型別
var 查詢條件={'sno':2019000000};
var 更新操作={$set:{'body.height':Math.floor(Math.random()*10+160)}}
db.students.update(查詢條件, 更新操作);
//驗證查詢
var 查詢條件={'sno':2019000000};
var 回傳條件={};
var result=db.students.findOne(查詢條件,回傳條件);
利用陣列進行更新操作,取出我們的資料,并將其轉換為字串型別的資料,然后利用$set:進行更新操作
//方法一:更新指定欄位(重點掌握)
result.body.height=String(result.body.height)
var 更新操作={$set:{'body.height':result.body.height}}
db.students.update(查詢條件, 更新操作);
原子操作知識回顧
$set
用來指定一個鍵并更新鍵值,若鍵不存在并創建,
{ $set : { field : value } }
$unset
用來洗掉一個鍵,
{ $unset : { field : 1} }
$inc
$inc可以對檔案的某個值為數字型(只能為滿足要求的數字)的鍵進行增減的操作,
{ $inc : { field : value } }
$push
把value追加到field里面去,field一定要是陣列型別才行,如果field不存在,會新增一個陣列型別加進去,
用法:
{ $push : { field : value } }
$pushAll
同$push,只是一次可以追加多個值到一個陣列欄位內,
{ $pushAll : { field : value_array } }
$pull
從陣列field內洗掉一個等于value值,
{ $pull : { field : _value } }
$addToSet
增加一個值到陣列內,而且只有當這個值不在陣列內才增加,
$pop
洗掉陣列的第一個或最后一個元素
{ $pop : { field : 1 } }
$rename
修改欄位名稱
{ $rename : { old_field_name : new_field_name } }
$bit
位操作,integer型別
{$bit : { field : {and : 5}}}
偏移運算子
> t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }
> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )
> t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }
//$exists 運算子,判斷欄位是否存在
db.test.insert({"name":'王小王'})
db.test.insert({"name":'王小王-123','gender':'男','age':19})
$exists:0(1)可以查詢出前面的欄位的存在性,如果存在那么屬性為1,如果不存在那么屬性就是0
//有name欄位
db.test.find({'name':{$exists:1}})
//無name欄位
db.test.find({'name':{$exists:0}})
$size:可以查詢這個欄位的資訊是否為空
db.test.insert({myname:[1,2,3]})
//查詢myname不為零的資訊,而且存在的
db.test.find({myname:{$not:{$size:0},$exists:1}})
var cursor =db.students.find()
for(var i=0;i<10;i++)
{
if(cursor.hasNext())
printjson(cursor.next())
}
列印輸出我們想要的資訊,有時候我們查詢回傳的是一個資訊,但是我們想要向Python列印那樣,格式化的輸出,這樣更有利于我們的理解和直觀查看資料的特性
語法類似于JavaScript的語法,和Python不是很一樣,但是邏輯思維douchabduo
var 查詢條件 = {'grade':2019,'class':1,'major':'大資料'};
var cursor = db.students.find(查詢條件);
for(var i=0; i<cursor.length(); i++) {
if (cursor[i].gender==0)
print("姓名:"+cursor[i].name+"\t身高:"+cursor[i].body.height+"\t性別:女");
};

本期的資料查詢案例就到這里,當我們學過大量的查詢語法之后我們發現和MySQL有一種比較類似的現象就是,要想根據實際的場景來撰寫代碼一定要有足夠的的語法和熟練度這樣才可以!
每文一語
喜歡沒有原因,因為快樂是最好的理由
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/273686.html
標籤:其他
