中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB語法實踐

發布時間:2020-02-29 21:04:54 來源:網絡 閱讀:644 作者:DBAspace 欄目:MongoDB數據庫

####簡單操作過程

基本查詢:

構造查詢數據:

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" }

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

    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,依次等價于</<=/>/>=/!=

--返回符合條件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" }

--返回指定的鍵值對條件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數據類型的查詢

    --在進行值為null數據的查詢時,所有值為null,以及不包括指定鍵的文檔均會被檢索出來

    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、數組數據查詢

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

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

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

--數組中所有包含banana的文檔都會被檢索出來

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

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

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

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

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

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

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

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

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

--匹配數組中指定下標元素的值,數組的起始下標是0

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

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

--可以通過$size獲取數組的長度,但是$size不能和比較操作符聯合使用

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的結果,不能直接使用$size,只能是添加一個額外的鍵表示數據中的元素數據,在操作數據中的元素時,需要同時更新size鍵的值

--為后面的實驗構造數據

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 }

--每次添加一個新元素,都要原子性的自增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返回數組中的部分數據。"$slice":2表示數組中的前兩個元素

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

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

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

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

--$slice : [2,1],表示從第二個2元素開始取1個,如果獲取數量大于2后面的元素數量,則取后面的全部數據

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、內嵌文檔查詢

    --當嵌入式文檔為數組時,需要$elemMatch操作符來幫助定位某一個元素匹配的情況,否則嵌入式文件將進行全部的匹配

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

    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問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黑水县| 久治县| 台南市| 宣城市| 依安县| 镶黄旗| 彭阳县| 南华县| 内乡县| 高阳县| 布拖县| 舞钢市| 长白| 延寿县| 广安市| 安福县| 新晃| 阿瓦提县| 弋阳县| 庄浪县| 栾城县| 兰考县| 邛崃市| 云龙县| 陆川县| 凯里市| 治多县| 那坡县| 沾化县| 微博| 贵德县| 通山县| 乌海市| 旬阳县| 黄石市| 乐亭县| 丽江市| 兰西县| 盐津县| 仙居县| 浦北县|