日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

MongoDB常用语句

發(fā)布時(shí)間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MongoDB常用语句 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  

記錄一下MongoDB常用語(yǔ)句,順帶與SQL做個(gè)簡(jiǎn)單的對(duì)比。

  1、查詢(xún)(find)

  (1)查詢(xún)所有結(jié)果

select * from articledb.article.find()

  (2)指定返回哪些鍵

select title, author from articledb.article.find({}, {"title": 1, "author": 1})

  (3)where條件

select * from article where title = "mongodb" db.article.find({"title": "mongodb"})

  (4)and條件

select * from article where title = "mongodb" and author = "god"db.article.find({"title": "mongodb", "author": "god"})

  (5)or條件

select * from article where title = "mongodb" or author = "god"db.article.find({"$or": [{"title": "mongodb"}, {"author": "god"}]})

  (6)比較條件

select * from article where read >= 100;db.article.find({"read": {"$gt": 100}})

  $gt(>)、$gte(>=)、$lt(<)、$lte(<=)

select * from article where read >= 100 and read <= 200db.article.find({"read": {"$gte": 100, "lte": 200}})

  (7)in條件

select * from article where author in ("a", "b", "c")db.article.find({"author": {"$in": ["a", "b", "c"]}})

  (8)like

select * from article where title like "%mongodb%"db.article.find({"title": /mongodb/})

  (9)count

select count(*) from articledb.article.count()

  (10)不等于

select * from article where author != "a"db.article.find({ "author": { "$ne": "a" }})

  (11)排序

  降序:

select * from article where type = "mongodb" order by read descdb.article.find({"type": "mongodb"}).sort({"read": -1})

  升序:

select * from article where type = "mongodb" order by read ascdb.article.find({"type": "mongodb"}).sort({"read": 1})

  findOne():除了只返回一個(gè)查詢(xún)結(jié)果外,使用方法與find()一樣。

  2、創(chuàng)建(insert)

insert into article(title, author, content) values("mongodb", "tg", "haha")db.article.insert({"title": "mongodb", "author": "tg", "content": "haha"})

  3、更新(update)

  (1)update()

  語(yǔ)法:

db.collecion.update(query, update[, options] )
  • query : 必選,查詢(xún)條件,類(lèi)似find中的查詢(xún)條件。

  • update : 必選,update的對(duì)象和一些更新的操作符(如$,$inc...)等

  • options:可選,一些更新配置的對(duì)象。

  upsert:可選,這個(gè)參數(shù)的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認(rèn)是false,不插入。

  multi:可選,mongodb 默認(rèn)是false,只更新找到的第一條記錄,如果這個(gè)參數(shù)為true,就把按條件查出來(lái)多條記錄全部更新。

  writeConcern:可選,拋出異常的級(jí)別。

  簡(jiǎn)單更新:

update article set title = "mongodb" where read > 100db.article.update({"read": {"$gt": 100}}, {"$set": { "title": "mongodb"}})

  (2)save()

db.article.save({_id: 123, title: "mongodb"})

  執(zhí)行上面的語(yǔ)句,如果集合中已經(jīng)存在一個(gè)_id為123的文檔,則更新對(duì)應(yīng)字段;否則插入。

  注:如果更新對(duì)象不存在_id,系統(tǒng)會(huì)自動(dòng)生成并作為新的文檔插入。

  (3)更新操作符

  MongoDB提供一些強(qiáng)大的更新操作符。

  更新特定字段($set):

update game set count = 10000 where _id = 123db.game.update({"_id": 123}, { "$set": {"count": 10000}})

  刪除特定字段($unset):

db.game.update({"_id":123}, {"$unset": {"author":1}})

  注:$unset指定字段的值只需是任意合法值即可。

  遞增或遞減($inc)

db.game.update({"_id": 123}, { "$inc": {"count": 10}}) // 每次count都加10

  注意:$inc對(duì)應(yīng)的字段必須是數(shù)字,而且遞增或遞減的值也必須是數(shù)字。

  數(shù)組追加($push):

db.game.update({"_id": 123}, { "$push": {"score": 123}})

  還可以一次追加多個(gè)元素:

db.game.update({"_id": 123}, {"$push": {"score": [12,123]}})

  注:追加字段必須是數(shù)組。如果數(shù)組字段不存在,則自動(dòng)新增,然后追加。

  一次追加多個(gè)元素($pushAll):

db.game.update({"_id": 123}, {"$pushAll": {"score": [12,123]}})

  追加不重復(fù)元素($addToSet):

  $addToSet類(lèi)似集合Set,只有當(dāng)這個(gè)值不在元素內(nèi)時(shí)才增加:

db.game.update({"_id": 123}, {"$addToSet": {"score": 123}})

  刪除元素($pop):

db.game.update({"_id": 123}, {"$pop": {"score": 1}}) ?// 刪除最后一個(gè)元素db.game.update({"_id": 123}, {"$pop": {"score": -1}}) ?// 刪除第一個(gè)元素

  注:$pop每次只能刪除數(shù)組中的一個(gè)元素,1表示刪除最后一個(gè),-1表示刪除第一個(gè)。

  刪除特定元素($pull):

db.game.update({"_id": 123}, {"$pull": {"score": 123}})

  上面的語(yǔ)句表示刪除數(shù)組score內(nèi)值等于123的元素。

  刪除多個(gè)特定元素($pullAll):

db.game.update({"_id": 123}, {"$pullAll": {score: [123,12]}})

  上面的語(yǔ)句表示刪除數(shù)組內(nèi)值等于123或12的元素。

  更新嵌套數(shù)組的值:

  使用數(shù)組下標(biāo)(從0開(kāi)始):

{address: [{place: "nanji", tel: 123}, {place: "dongbei", tel: 321}] } db.game.update({"_id": 123}, {"$set": {"address.0.tel": 213}})

  如果你不知道要更新數(shù)組哪項(xiàng),我們可以使用$操作符( $表示自身,也就是按查詢(xún)條件找出的數(shù)組里面的項(xiàng)自身,而且只會(huì)應(yīng)用找到的第一條數(shù)組項(xiàng)):

db.game.update({"address.place": "nanji"}, {"$set": {"address.$.tel": 123}})

  在上面的語(yǔ)句中,$就是查詢(xún)條件{"address.place": "nanji"}的查詢(xún)結(jié)果,也就是{place: "nanji", tel: 123},所以{"address.$.tel": 123}也就是{"address.{place: "nanji", tel: 123}.tel": 123}

  4、刪除(remove)

  刪除所有文檔:

delete from articledb.article.remove()

  刪除指定文檔:

delete from article where title = "mongodb"db.article.remove({title: "mongodb"})

  MongoDB特有的語(yǔ)句

  1. 數(shù)組查詢(xún)

  (1)數(shù)組的普通查詢(xún)

  假如type是["mongodb", "javascript"]:

db.article.find({"type": "mongodb"})

  上面的語(yǔ)句可以匹配成功。

  (2)多個(gè)元素的查詢(xún)

db.article.find({"type": {"$all": ["mongodb", "javascript"]}})

  只有type數(shù)組同時(shí)存在mongodb和javascript才會(huì)匹配。

  (3)限制數(shù)組長(zhǎng)度查詢(xún)

db.article.find({"type": {"$size": 2}})

  只有數(shù)組的長(zhǎng)度是2才會(huì)匹配

  注:type必須是數(shù)組

  (4)返回特定數(shù)量

  當(dāng)$slice的參數(shù)是一個(gè)時(shí),表示返回的數(shù)量;當(dāng)是一個(gè)數(shù)組時(shí),第一個(gè)參數(shù)表示偏移量,第二個(gè)表示返回的數(shù)量:

db.article.find({"type": {"$slice": 1}}) // 返回第1個(gè)db.article.find({"type": {"$slice": -1}}) ?// 返回最后一個(gè)db.article.find({"type": {"$slice": [20, 10]}}) ?// 從第21個(gè)開(kāi)始,返回10個(gè),也就是21~30

  注:$slice針對(duì)的是數(shù)組

  (5)元素匹配

  如果文檔中有一個(gè)字段的值是數(shù)組,可以使用$elemMatch來(lái)匹配數(shù)組內(nèi)的元素:

{kown: [{ a: 2, b: 4}, 10, { a: 4}, {b:10}] } db.article.find({"kown": { "$elemMatch": {a: 1, b: {"$gt": 2}}}})

  只有a=1且b>2才會(huì)匹配。

  2. 取模($mod)

  比如我們要匹配 read % 5 == 1:

db.article.find({"read": {$mod: [5, 1]}})

  3. 是否存在($exists)

  如果我們要判斷l(xiāng)ove字段是否存在,可以這樣:

db.article.find({"love": {"$exists": true}}) ?// 如果存在字段love,就返回

  我們也可以判斷不存在:

db.article.find({"love": {"$exists": false}}) // 如果不存在字段love,就返回

  4. 正則表達(dá)式

  mongodb支持正則表達(dá)式,使用方法與正則字面量一樣:

db.article.find({"title": /mongodb/i}) ?// i是忽略大小寫(xiě)

  5. 類(lèi)型查詢(xún)

  我們可以根據(jù)字段類(lèi)型來(lái)返回?cái)?shù)據(jù):

db.article.find({"comments": {"$type": 4}})

  只有當(dāng)comments的類(lèi)型是數(shù)組才匹配

  注:更多類(lèi)型的數(shù)值可以參考這里:mongodb $type

  6. 內(nèi)嵌文檔

  mongodb是允許內(nèi)嵌文檔的,而且要查詢(xún)內(nèi)嵌文檔也很簡(jiǎn)單(使用點(diǎn)語(yǔ)法):

{address: { name: "nanji" } } db.article.find({"address.name": "nanji"})

  上面的語(yǔ)句是查詢(xún)comments中的author。

  數(shù)組也可以采取點(diǎn)語(yǔ)法:

{comments: [{title: "mongodb"}, {title: "javascript"}] } db.article.find({"comments.title": "mongodb"})

  7. 取反

  $not是元語(yǔ)句,即可以用在任何其他條件之上:

db.article.find({"author": {"$not": /mongodb/i}})

  只要使用$not操作符,就表示取反。

  MongoDB常用方法

  1. 控制返回?cái)?shù)量(limit)

db.article.find().limit(10)

  返回10條數(shù)據(jù)

  2. 略過(guò)數(shù)量

db.article.find().skip(5)

  略過(guò)前5條數(shù)據(jù),也就是從第6條開(kāi)始返回。

可以結(jié)合limit()和skip()來(lái)達(dá)到分頁(yè)效果:

select * from article limit 10, 20db.article.find().skip(10).limit(20)

  3. 統(tǒng)計(jì)

  返回匹配數(shù)據(jù)的長(zhǎng)度:

db.article.find().count()

  4. 格式化

  pretty()方法可以以格式化的方式顯示所有文檔:

db.article.find().pretty()

  5. 刪除集合

  當(dāng)你要?jiǎng)h除一個(gè)集合中的所有文檔時(shí),直接刪除一個(gè)集合效率會(huì)更高:

db.article.drop()


總結(jié)

以上是生活随笔為你收集整理的MongoDB常用语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。