本篇文章給大家?guī)淼膬?nèi)容是關(guān)于mongodb中常用的語句總結(jié),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對你有所幫助。
如果覺得 mongodb 語句不太好理解,可以和 SQL 語句進(jìn)行對比,學(xué)起來要容易很多。
1. 查詢(find)
查詢所有結(jié)果
select * from article db.article.find()
指定返回哪些鍵
select title, author from article db.article.find({}, {"title": 1, "author": 1})
where條件
select * from article where title = "mongodb" db.article.find({"title": "mongodb"})
and條件
select * from article where title = "mongodb" and author = "god" db.article.find({"title": "mongodb", "author": "god"})
or條件
select * from article where title = "mongodb" or author = "god" db.article.find({"$or": [{"title": "mongodb"}, {"author": "god"}]})
比較條件
select * from article where read >= 100; db.article.find({"read": {"$gt": 100}})
> $gt(>)、$gte(>=)、$lt(<)、$lte(<=)
select * from article where read >= 100 and read <= 200 db.article.find({"read": {"$gte": 100, "lte": 200}})
in條件
select * from article where author in ("a", "b", "c") db.article.find({"author": {"$in": ["a", "b", "c"]}})
like
select * from article where title like "%mongodb%" db.article.find({"title": /mongodb/})
count
select count(*) from article db.article.count()
不等于
select * from article where author != "a" db.article.find({ "author": { "$ne": "a" }})
排序
升序:
select * from article where type = "mongodb" order by read desc db.article.find({"type": "mongodb"}).sort({"read": -1})
降序:
select * from article where type = "mongodb" order by read asc db.article.find({"type": "mongodb"}).sort({"read": 1})
findOne():除了只返回一個(gè)查詢結(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)
update()
語法:
db.collecion.update(query, update[, options] )
query : 必選,查詢條件,類似find中的查詢條件。 update : 必選,update的對象和一些更新的操作符(如$,$inc...)等 options:可選,一些更新配置的對象。 upsert:可選,這個(gè)參數(shù)的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認(rèn)是false,不插入。 multi:可選,mongodb 默認(rèn)是false,只更新找到的第一條記錄,如果這個(gè)參數(shù)為true,就把按條件查出來多條記錄全部更新。 writeConcern:可選,拋出異常的級(jí)別。
簡單更新:
update article set title = "mongodb" where read > 100 db.article.update({"read": {"$gt": 100}}, {"$set": { "title": "mongodb"}})
save()
db.article.save({_id: 123, title: "mongodb"})
執(zhí)行上面的語句,如果集合中已經(jīng)存在一個(gè)_id為123的文檔,則更新對應(yīng)字段;否則插入。
注:如果更新對象不存在_id,系統(tǒng)會(huì)自動(dòng)生成并作為新的文檔插入。
更新操作符
MongoDB提供一些強(qiáng)大的更新操作符。
更新特定字段($set):
update game set count = 10000 where _id = 123 db.game.update({"_id": 123}, { "$set": {"count": 10000}})
刪除特定字段($unset):
注:$unset指定字段的值只需是任意合法值即可。
遞增或遞減($inc)
db.game.update({"_id": 123}, { "$inc": {"count": 10}}) // 每次count都加10
>?注意:$inc對應(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類似集合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}})
上面的語句表示刪除數(shù)組score內(nèi)值等于123的元素。
刪除多個(gè)特定元素($pullAll):
db.game.update({"_id": 123}, {"$pullAll": {score: [123,12]}})
上面的語句表示刪除數(shù)組內(nèi)值等于123或12的元素。
更新嵌套數(shù)組的值:
使用數(shù)組下標(biāo)(從0開始):
{ ????address:?[{place:?"nanji",?tel:?123},?{place:?"dongbei",?tel:?321}] }
db.game.update({"_id": 123}, {"$set": {"address.0.tel": 213}})
如果你不知道要更新數(shù)組哪項(xiàng),我們可以使用$操作符(?$表示自身,也就是按查詢條件找出的數(shù)組里面的項(xiàng)自身,而且只會(huì)應(yīng)用找到的第一條數(shù)組項(xiàng)):
db.game.update({"address.place": "nanji"}, {"$set": {"address.$.tel": 123}})
在上面的語句中,$就是查詢條件{“address.place”:?“nanji”}的查詢結(jié)果,也就是{place:?“nanji”,?tel:?123},所以{“address.$.tel”:?123}也就是{“address.{place:?“nanji”,?tel:?123}.tel”:?123}
4. 刪除(remove)
刪除所有文檔:
delete from article db.article.remove()
刪除指定文檔:
delete from article where title = "mongodb" db.article.remove({title: "mongodb"})