MongoDB資料庫的增刪查改 1.插入數據 語法: db.集合名稱.insert(document) db.table_name.insert({name:'gj',gender:1}) db.table_name.insert({_id:"20170101",name:'gj',gender:1 ...
MongoDB資料庫的增刪查改
1.插入數據
語法:
db.集合名稱.insert(document)
db.table_name.insert({name:'gj',gender:1})
db.table_name.insert({_id:"20170101",name:'gj',gender:1})
插⼊⽂檔時, 如果不指定_id參數,MongoDB會為⽂檔分配⼀個唯⼀的ObjectId
使用insert時如果指定的_id在數據表中已經存在,則插入數據失敗
例子:
> db.test_table01.insert({_id:1000001,name:"xiaobing",age:30}) # 向資料庫中插入一條_id為1000001的數據
WriteResult({ "nInserted" : 1 })
> db.test_table01.insert({_id:1000001,name:"xiaoming",age:40}) # 再次向資料庫中插入一條_id為1000001的數據,會提示失敗
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: study_test.test_table01 index: _id_ dup key: { : 1000001.0 }"
}
})
2.保存數據
語法:
db.集合名稱.save(document)
如果⽂檔的_id已經存在則修改文檔,如果⽂檔的_id不存在則添加,
類似於Django中的update_or_create
例子:
> db.test_table01.find()
{ "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaowang", "age" : 10 }
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaohong", "age" : 20 }
{ "_id" : 1000001, "name" : "xiaobing", "age" : 30 }
> db.test_table01.save({_id:1000001,name:"xiaoming",age:40}) # 向資料庫中保存_id為1000001的數據,會更新已經的數據
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test_table01.find()
{ "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaowang", "age" : 10 }
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaohong", "age" : 20 }
{ "_id" : 1000001, "name" : "xiaoming", "age" : 40 }
3.簡單查詢
語法:
db.集合名稱.find({條件⽂檔}) # 查詢所有符合條件的文檔
db.集合名稱.findOne({條件⽂檔}) # 根據條件查詢文檔,只返回第⼀個
db.集合名稱.find({條件⽂檔}).pretty() # 根據條件查詢文檔,並將輸出結果格式化
4.更新
語法:
db.集合名稱.update(<query> ,<update>,{multi: <boolean>})
參數說明:
query:查詢條件
update:更新操作符
multi:可選,預設是false,表示只更新找到的第⼀條記錄,值為true表示把滿⾜query條件的⽂檔全部更新
"multi update only works with $ operators":只有替換後的欄位前面加上"$"時,才可以一次性更新多條數據
例子:
> db.userinfo.find().pretty() # userinfo資料庫里有5條數據
{
"_id" : ObjectId("5c94f37d25000fc9936a9759"),
"country" : "china",
"province" : "sh",
"userid" : "a"
}
{
"_id" : ObjectId("5c94f38425000fc9936a975a"),
"country" : "china",
"province" : "sh",
"userid" : "b"
}
{
"_id" : ObjectId("5c94f38625000fc9936a975b"),
"country" : "china",
"province" : "sh",
"userid" : "c"
}
{
"_id" : ObjectId("5c94f39325000fc9936a975c"),
"country" : "china",
"province" : "bj",
"userid" : "da"
}
{
"_id" : ObjectId("5c94f39925000fc9936a975d"),
"country" : "china",
"province" : "bj",
"userid" : "fa"
}
> db.userinfo.update({country:"china"},{set:{country:"zhongguo"}}) # 把country等於china的整條數據替換掉,只替換一條數據
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.userinfo.find() # 第一條數據被替換,只有_id欄位沒有改變
{ "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
{ "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "china", "province" : "sh", "userid" : "b" }
{ "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "china", "province" : "sh", "userid" : "c" }
{ "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "china", "province" : "bj", "userid" : "da" }
{ "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "china", "province" : "bj", "userid" : "fa" }
> db.userinfo.update({country:"china"},{$set:{country:"zhongguo"}}) # 把country等於china的欄位更新為country等於zhongguo,只更新一條數據
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.userinfo.find()
{ "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
{ "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zhongguo", "province" : "sh", "userid" : "b" }
{ "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "china", "province" : "sh", "userid" : "c" }
{ "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "china", "province" : "bj", "userid" : "da" }
{ "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "china", "province" : "bj", "userid" : "fa" }
> db.userinfo.update({country:"china"},{$set:{country:"zhongguo"}},{multi:true}) # 把country等於china的欄位更新為country等於zhongguo,multi的值為true,所以更新所有符合條件的數據
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.userinfo.find()
{ "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
{ "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zhongguo", "province" : "sh", "userid" : "b" }
{ "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "zhongguo", "province" : "sh", "userid" : "c" }
{ "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "zhongguo", "province" : "bj", "userid" : "da" }
{ "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "zhongguo", "province" : "bj", "userid" : "fa" }
> db.userinfo.update({country:"zhongguo"},{set:{country:"zh_CN"}},{multi:true}) # multi的值為true,但是set前沒有加"$",所以此次更新操作沒有更新數據
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 9,
"errmsg" : "multi update only works with $ operators"
}
})
> db.userinfo.update({country:"zhongguo"},{$set:{country:"zh_CN"}},{multi:true}) # multi的值為true,set前加了"$",所以把所有文檔的country欄位更新為"zh_CN"
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
> db.userinfo.find()
{ "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
{ "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zh_CN", "province" : "sh", "userid" : "b" }
{ "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "zh_CN", "province" : "sh", "userid" : "c" }
{ "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "zh_CN", "province" : "bj", "userid" : "da" }
{ "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "zh_CN", "province" : "bj", "userid" : "fa" }
5.刪除文檔
語法:
db.集合名稱.remove(<query>,{justOne: <boolean>})
參數說明:
query:可選,刪除⽂檔的條件
justOne:可選, 如果設為true或1, 則只刪除⼀條, 預設false, 表示刪除多條
例子:
> db.test_table01.find()
{ "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaoqiang", "age" : 10 }
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : 1000001, "name" : "xiaobei" }
{ "_id" : ObjectId("5c939dd14c9ce97c5b78a0dc"), "name" : "xiaoqiang", "age" : 20 }
{ "_id" : ObjectId("5c939dd44c9ce97c5b78a0dd"), "name" : "xiaoqiang", "age" : 30 }
{ "_id" : ObjectId("5c939dd74c9ce97c5b78a0de"), "name" : "xiaoqiang", "age" : 40 }
> db.test_table01.remove({name:"xiaobei"}) # 刪除所有name為xiaobei的數據
WriteResult({ "nRemoved" : 1 })
> db.test_table01.find()
{ "_id" : ObjectId("5c939a4f4c9ce97c5b78a0da"), "name" : "xiaoqiang", "age" : 10 }
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c939dd14c9ce97c5b78a0dc"), "name" : "xiaoqiang", "age" : 20 }
{ "_id" : ObjectId("5c939dd44c9ce97c5b78a0dd"), "name" : "xiaoqiang", "age" : 30 }
{ "_id" : ObjectId("5c939dd74c9ce97c5b78a0de"), "name" : "xiaoqiang", "age" : 40 }
> db.test_table01.remove({name:"xiaoqiang"},{justOne:true}) # 刪除一條name等於xiaoqiang的數據
WriteResult({ "nRemoved" : 1 })
> db.test_table01.find()
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c939dd14c9ce97c5b78a0dc"), "name" : "xiaoqiang", "age" : 20 }
{ "_id" : ObjectId("5c939dd44c9ce97c5b78a0dd"), "name" : "xiaoqiang", "age" : 30 }
{ "_id" : ObjectId("5c939dd74c9ce97c5b78a0de"), "name" : "xiaoqiang", "age" : 40 }
> db.test_table01.remove({name:"xiaoqiang"}) # 刪除所有name等於xiaoqiang的數據,共刪除了3條數據
WriteResult({ "nRemoved" : 3 })
> db.test_table01.find()
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
6.⽐較運算符查詢
MongoDB資料庫中的⽐較運算符有:
= 等於,預設是等於判斷,沒有運算符
$lt ⼩於
$lte ⼩於等於
$gt ⼤於
$gte ⼤於等於
$ne 不等於
例子:
> db.test_table01.insert({name:"喬峰",hometown:"丐幫",gender:"male",age:32}) # 向數據表中插入四條數據
WriteResult({ "nInserted" : 1 })
> db.test_table01.insert({name:"段譽",hometown:"雲南大理",gender:"male",age:22})
WriteResult({ "nInserted" : 1 })
> db.test_table01.insert({name:"虛竹",hometown:"少林寺",gender:"male",age:28})
WriteResult({ "nInserted" : 1 })
> db.test_table01.insert({name:"王語嫣",hometown:"燕子塢",gender:"female",age:18})
WriteResult({ "nInserted" : 1 })
> db.test_table01.find()
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
> db.test_table01.find({age:{$lt:20}}) # 查找年齡小於20歲的所有數據
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
> db.test_table01.find({age:{$lte:25}}) # 查找年齡小於等於25歲的所有數據
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
> db.test_table01.find({age:{$gt:20}}) # 查找年齡大於20歲的所有數據
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
> db.test_table01.find({age:{$gte:30}}) # 查找年齡大於等於30歲的所有數據
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
> db.test_table01.find({age:{$ne:18}}) # 查找年齡不等於18歲的所有數據
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
7.邏輯運算符查詢
語法:
and 條件與,多個條件共同查詢
or 條件或,符合一個條件與可
例子:
> db.test_table01.find({age:22,gender:"male"}) # 查找所有年齡為22,性別為男的人
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
> db.test_table01.find({age:{$gt:20},gender:"male"}) # 查找所有年齡大於20,性別為男的人
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
> db.test_table01.find({$or:[{age:{$gt:20},gender:"male"}]}) # 查找所有年齡大於20或者性別為男的人
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
> db.test_table01.find({$or:[{age:{$gte:20},gender:"male"}]}) # 查找所有年齡大於等於20或者性別為男的人
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
8.範圍運算符查詢
語法:
$in 在範圍之內
$nin 不在範圍之內
例子:
> db.test_table01.find({age:{$in:[18,28,38]}}) # 查找年齡為18或者28或者38的人
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
> db.test_table01.find({age:{$nin:[18,28,38]}}) # 查找年齡不等於18或者28或者38的人
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
9.正則表達式語句查詢
語法:
// 後接正則表達式進行查詢
$regex 後接正則表達式進行查詢
例子:
> db.test_table01.find({name:{$regex:'^王'}}) # 查找姓王的所有人
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
10.limit和skip
limit() ⽤於讀取指定數量的⽂檔
skip() ⽤於跳過指定數量的⽂檔
語法:
db.集合名稱.find({條件⽂檔}).skip(number) # 按條件查詢,跳過number條數據
db.集合名稱.find({條件⽂檔}).limit(number) # 按條件查詢,取前number條數據
db.集合名稱.find({條件⽂檔}).limit(10).skip(5) # 按條件查詢後,取第6到第10條數據
db.集合名稱.find({條件⽂檔}).skip(5).limit(4) # 按條件查詢後,取第6條到和9條數據
例子:
> db.test_table01.find({gender:"male"}) # 查詢所有的男性
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
> db.test_table01.find({gender:"male"}).limit(2) # 查詢所有的男性,取前兩條數據
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
> db.test_table01.find({gender:"male"}).skip(2) # 查詢所有的男性,跳過前2條數據
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
11.投影
語法:
db.集合名稱.find({},{欄位名稱:1,...}) # 查詢結果,只選擇必要的欄位
參數為欄位與值,值為1表示顯示,值為0不顯
對於_id列預設是顯示的, 如果不顯示需要明確設置為0
例子:
> db.test_table01.find({gender:"male"},{name:1,hometown:1}) # 查詢所有的男性,顯示_id,name和hometown
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫" }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理" }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺" }
> db.test_table01.find({gender:"male"},{name:1}) # 查詢所有男性,顯示name和_id欄位
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰" }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽" }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹" }
> db.test_table01.find({gender:"male"},{hometown:1}) # 查詢所有男性,顯示_id和hometown欄位
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "hometown" : "丐幫" }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "hometown" : "雲南大理" }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "hometown" : "少林寺" }
> db.test_table01.find({gender:"male"},{hometown:1,_id:0}) # 查詢所有男性,顯示hometown,不顯示_id欄位
{ "hometown" : "丐幫" }
{ "hometown" : "雲南大理" }
{ "hometown" : "少林寺" }
> db.test_table01.find({},{name:1,age:1}) # 查詢所有男性,顯示_id和name,age欄位
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "age" : 28 }
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "age" : 18 }
> db.test_table01.find({},{name:1,age:1,_id:0}) # 查詢所有男性,顯示name,age欄位
{ "name" : "xiaozhao", "age" : 20 }
{ "name" : "喬峰", "age" : 32 }
{ "name" : "段譽", "age" : 22 }
{ "name" : "虛竹", "age" : 28 }
{ "name" : "王語嫣", "age" : 18 }
12.排序
sort() 對查詢結果進⾏排序
語法:
db.集合名稱.find().sort({欄位:1,...})
參數1為升序排列
參數-1為降序排列
例子:
> db.test_table01.find().sort({age:1}) # 查詢所有數據,按age欄位升序排列
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
> db.test_table01.find().sort({age:-1}) # 查詢所有數據,按age欄位降序排列
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
> db.test_table01.find().sort({name:-1}) # 查詢所有數據,按name欄位降序排列
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
> db.test_table01.find().sort({hometown:1}) # 查詢所有數據,按hometown欄位升序排列
{ "_id" : ObjectId("5c939adc4c9ce97c5b78a0db"), "name" : "xiaozhao", "age" : 20 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
{ "_id" : ObjectId("5c93a1e74c9ce97c5b78a0e4"), "name" : "段譽", "hometown" : "雲南大理", "gender" : "male", "age" : 22 }
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
{ "_id" : ObjectId("5c93a1fd4c9ce97c5b78a0e6"), "name" : "王語嫣", "hometown" : "燕子塢", "gender" : "female", "age" : 18 }
> db.test_table01.find({age:{$gt:25}}).sort({age:1}) # 查詢age大於25的所有數據,按age欄位升序排列
{ "_id" : ObjectId("5c93a1f14c9ce97c5b78a0e5"), "name" : "虛竹", "hometown" : "少林寺", "gender" : "male", "age" : 28 }
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }
13.統計個數
count() 統計結果集中⽂檔條數
語法:
db.集合名稱.find({查詢條件}).count()
db.集合名稱.count({查詢條件})
例子:
> db.test_table01.find({}).count() # 查詢所有數據的數量
5
> db.test_table01.find({age:{$gt:25}}).count() # 查詢age大於25的所有數據的總數
2
> db.test_table01.find({gender:"male"}).count() # 查詢gender等於male的所有數據的總數
3
14.去重
distinct() 對數據進⾏去重
語法:
db.集合名稱.distinct('去重欄位',{條件})
例子:
> db.test_table01.distinct("gender") # 對gemder欄位進行去重
[ "male", "female" ]
> db.test_table01.distinct("gender",{age:{$gt:20}}) # 對age大於20的所有數據的gender欄位信息進行去重
[ "male" ]
> db.test_table01.distinct("age") # 對所有數據的age欄位進行去重
[ 20, 32, 22, 28, 18 ]
> db.test_table01.distinct("name") # 對所有數據的name欄位進行去重
[ "xiaozhao", "喬峰", "段譽", "虛竹", "王語嫣" ]
15.⾃定義查詢
語法:
使⽤$where後⾯寫⼀個函數, 返回滿⾜條件的數據
例子:
> db.test_table01.find({$where:function(){return this.age > 30}}) # 查詢age大於30的所有數據
{ "_id" : ObjectId("5c93a1d64c9ce97c5b78a0e3"), "name" : "喬峰", "hometown" : "丐幫", "gender" : "male", "age" : 32 }