MongoDB資料庫(二):增刪查改

来源:https://www.cnblogs.com/renpingsheng/archive/2019/03/23/10582698.html
-Advertisement-
Play Games

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 }

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • CREATE TRIGGER m_trigger AFTER UPDATE ON table1 FOR EACH ROW BEGIN IF( old.status!= new.status) THEN SELECT COUNT(id) FROM table1 WHERE stats='Y' INTO ...
  • MySQL資料庫備份與還原 資料庫備份也叫SQL數據備份,備份的結果都是SQL指令。 在MySQL中專門提供了一個用於資料庫備份的SQL客戶端:mysqldump.exe(MySql document copy),sql備份是一種MySQL非常常見的備份與還原方式,SQL備份不只是備份數據,還備份對 ...
  • 安裝環境:CentOS 6.5 MySQL 5.7.22 MHA 0.56 1、找不到mysql 命令 Sat Mar 23 07:17:50 2019 - [info] Connecting to [email protected](server2:22).. Checking slave r ...
  • 一、HDFS運行機制 概述:用戶的文件會被切塊後存儲在多台datanode節點中,並且每個文件在整個集群中存放多個副本,副本的數量可以通過修改配置自己設定。 HDFS:Hadoop Distributed file system,分散式文件系統。 HDFS的機制: HDFS集群中,有兩種節點,分別為 ...
  • Mysql資料庫基礎學習筆記 1.mysql查看當前登錄的賬戶名以及資料庫 一、單表查詢 1、創建資料庫yuzly,創建表fruits 創建表 插入內容 2、查詢所有欄位 3.查詢指定欄位 4.帶IN關鍵字的查詢 IN關鍵字:IN(xx,yy,...) 滿足條件範圍內的一個值即為匹配項 5.帶BET ...
  • 寫了一個釘釘發送消息的類, 要發送用友等審核單據信息, 模式: 釘釘發消息功能在webservice中, 用友消息列表中有新消息時,採用觸發器執行webservice. 在測試中 ,功能正常 ,但將在漢字發送到手機端後,顯示亂碼. 其實這種模式不合理, 這個亂碼問題也不解決了.下麵是觸發器中的代碼: ...
  • 這篇主要介紹一下database link由於連接資料庫的方式不同遇到的一些問題,我們知道連接ORACLE伺服器的模式一般有兩種方式:專用伺服器連接(dedicated server)和共用伺服器連接(shared server)。這次遇到的案例服務是dblink跟連接ORACLE方式有關,當然本質... ...
  • window下navicat錯誤提示:1251-Client does not support authentication protocol requested by server; consider upgrading MySQL client ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...