溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MongoDB語法實(shí)踐

發(fā)布時(shí)間:2020-02-29 21:04:54 來源:網(wǎng)絡(luò) 閱讀:635 作者:DBAspace 欄目:MongoDB數(shù)據(jù)庫

####簡(jiǎn)單操作過程

基本查詢:

構(gòu)造查詢數(shù)據(jù):

db.test.insert({name:"stephen",age:35,genda:"male",email:"stephen@hotmail.com"})

db.test.insert({name:"Stephen",age:35,genda:"male",email:"stephen@hotmail.com"})

db.test.insert({name:"stephen1",age:35,genda:"male",email:"stephen@hotmail.com"})

db.test.insert({name:"stephen",age:36,genda:"male",email:"stephen@hotmail.com"})

db.test.insert({name:"stephen",age:37,genda:"male",email:"stephen@hotmail.com"})

--多條件查詢,等同于SQL語句的where name = "stephen" and age = 35

db.test.find({name:"stephen",age:35})

{ "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

   --返回指定的文檔鍵值對(duì),只返回name和age鍵值對(duì)

    db.test.find({},{name:1,age:1})

    { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35 }

    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36 }

    { "_id" : ObjectId("59f1ae8a88f3edba94091896"), "name" : "stephen", "age" : 37 }

2、查詢條件

    MongoDB提供了一組比較操作符:$lt/$lte/$gt/$gte/$ne,依次等價(jià)于</<=/>/>=/!=

--返回符合條件age >= 18 && age <= 40的文檔

db.test.find({age:{$gte:35,$lte:36}})

    { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36, "genda" : "male", "email" : "stephen@hotmail.com" }

--返回指定的鍵值對(duì)條件age >= 18 && age <= 40的文檔

db.test.find({age:{$gte:35,$lte:36}},{name:1,age:1})

{ "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35 }

    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36 }

--返回條件符合name != "stephen1"

db.test.find({name:{$ne:"stephen"}})

    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

    --$in等同于SQL中的in,下面的示例等同于SQL中的in ("stephen","stephen1")

db.test.find({name:{$in:["stephen","stephen2"]}})

--下面的示例等同于name = "stephen1" or age = 35

db.test.find({$or:[{name:"stephen1"},{age:36}]})

{ "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36, "genda" : "male", "email" : "stephen@hotmail.com" }

    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

--下面的示例演示了如何混合使用$or和$in

db.test.find({$or:[{name:{$in:["stephen1","setphen12"]}},{age:36}]})

3、NULL數(shù)據(jù)類型的查詢

    --在進(jìn)行值為null數(shù)據(jù)的查詢時(shí),所有值為null,以及不包括指定鍵的文檔均會(huì)被檢索出來

    db.test.find({"x":null})

db.test.find({a:null})

    { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36, "genda" : "male", "email" : "stephen@hotmail.com" }

    { "_id" : ObjectId("59f1ae8a88f3edba94091896"), "name" : "stephen", "age" : 37, "genda" : "male", "email" : "stephen@hotmail.com" }

    { "_id" : ObjectId("59f1b1fd88f3edba94091897"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

--再有就是通過$exists判斷指定鍵是否存在

db.test.find({x:{$in:[null],$exists:true}})

    { "_id" : ObjectId("59f1c40b88f3edba94091899"), "x" : null }

4、正則查詢

    db.test.find()

--正則語法

db.test.find({name:/1/})

    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }

--i表示忽略大小寫

db.test.find({name:/stephen?/i})

5、數(shù)組數(shù)據(jù)查詢

    db.wqq.insert({name:["aa","bb","cc"]})

db.wqq.insert({name:["aa","ee","cc"]})

    db.wqq.insert({name:["ff","ee","cc"]})

--數(shù)組中所有包含banana的文檔都會(huì)被檢索出來

db.wqq.find({name:"aa"})

{ "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ] }

    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc" ] }

--檢索數(shù)組中需要包含多個(gè)元素的情況,這里使用$all。下面的示例中,數(shù)組中必須同時(shí)包含ff和ee

db.wqq.find({name:{$all:["ff","cc"]}})

{ "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ] }

--精確匹配即被檢索出來的文檔,name值中的數(shù)組數(shù)據(jù)必須和查詢條件完全匹配,即不能多,也不能少,順序也必須保持一致

    db.wqq.find({name:["ff","ee","cc"]})

{ "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ] }

--匹配數(shù)組中指定下標(biāo)元素的值,數(shù)組的起始下標(biāo)是0

db.wqq.find({"name.1":"bb"})

    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ] }

--可以通過$size獲取數(shù)組的長(zhǎng)度,但是$size不能和比較操作符聯(lián)合使用

db.wqq.find({"name":{$size:3}})

    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ] }

    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc" ] }

    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ] }

--如果需要檢索size > n的結(jié)果,不能直接使用$size,只能是添加一個(gè)額外的鍵表示數(shù)據(jù)中的元素?cái)?shù)據(jù),在操作數(shù)據(jù)中的元素時(shí),需要同時(shí)更新size鍵的值

--為后面的實(shí)驗(yàn)構(gòu)造數(shù)據(jù)

db.wqq.update({}, {"$set": {"size":3}},false,true)

    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

    > db.wqq.find()

    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ], "size" : 3 }

    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc" ], "size" : 3 }

    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ], "size" : 3 }

--每次添加一個(gè)新元素,都要原子性的自增size一次

db.wqq.update({},{$push:{name:123},$inc:{size:1}},0,1)

    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

    > db.wqq.find()

    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc", 123 ], "size" : 4 }

    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc", 123 ], "size" : 4 }

    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc", 123 ], "size" : 4 }

--通過$slice返回?cái)?shù)組中的部分?jǐn)?shù)據(jù)。"$slice":2表示數(shù)組中的前兩個(gè)元素

db.wqq.find({},{name:{$slice:2},size:0})   //若為負(fù)數(shù)則是最后2個(gè)元素

    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb" ] }

    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee" ] }

    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee" ] }

--$slice : [2,1],表示從第二個(gè)2元素開始取1個(gè),如果獲取數(shù)量大于2后面的元素?cái)?shù)量,則取后面的全部數(shù)據(jù)

db.wqq.find({},{name:{$slice:[2,1]}})

db.wqq.find({},{name:{$slice:[2,1]},size:0})

   { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "cc" ] }

   { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "cc" ] }

   { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "cc" ] }

6、內(nèi)嵌文檔查詢

    --當(dāng)嵌入式文檔為數(shù)組時(shí),需要$elemMatch操作符來幫助定位某一個(gè)元素匹配的情況,否則嵌入式文件將進(jìn)行全部的匹配

    --即檢索時(shí)需要將所有元素都列出來作為查詢條件方可

    db.aa.insert({comments:[{author:"sharesoe",score:3},{author:"mary",score:6}]})

    WriteResult({ "nInserted" : 1 })

    > db.aa.find()

    { "_id" : ObjectId("59f1cc8e88f3edba9409189f"), "comments" : [ { "author" : "sharesoe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }

  --

db.aa.find({comments:{$elemMatch:{author:"sharesoe",score:{$gte:3}}}})

    { "_id" : ObjectId("59f1cc8e88f3edba9409189f"), "comments" : [ { "author" : "sharesoe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI