mongo操作例子介紹

2021-09-28 13:47:43 字數 1462 閱讀 5867

本篇文章主要進行mongo增刪改查的例子介紹。

插入:預設情況下insert不檢查文件是否插入過了

db.foo.insert()

刪除:db.users.remove()

db.mailing.remove() ##刪除mailing集合中所有opt-out為true的

更新:db.classes.update(' at position 6: gt:20}̲},}) #把count大於20的class name修改為c3

db.students.update(,' at position 12: inc:}̲) ##將…set:}) ## 把chenzhou的年齡設為23歲

db.students.update(,' at position 14: unset:}̲) ## 將c…rename:}) ##把chenzhou記錄的name欄位重新命名為sname

查詢:db.users.find() 查詢所有「age」的值為27的文件

db.users.find() 查詢所有使用者名為「joe」且年齡為27的使用者

db.users.find({},) 對於查詢到的結果只返回使用者集合中的「username」和「email」鍵

db.users.find(}) 查詢在18~30歲(含)的使用者

db.users.find(' at position 10: ne":"joe"}̲}) 查詢所有名字不為…in」:[725,542,390]}}) 查詢ticket_no的值為:725、542、390的文件

db.users.find(' at position 18: …":[12345,"joe"]}̲}) 查詢user_…nin」:[725,542]}}) 查詢ticket_no的值不為:725、542的文件

db.raffle.find(,]}) 查詢ticket_no的值為725或者winner為true的文件

db.raffle.find(', got 'eof' at end of input: …},]}) 查詢ticket_no的值為:725、542、390的文件或者winner為true的文件

db.users.find() 查詢所有名為joe或者joe的使用者 引數i是忽略大小寫

db.blog.find(', got 'eof' at end of input: …joe","score":}}}) 匹配內嵌文件

db.blog.find().limit(3) 只展示前3條資料

db.blog.find().skip(3) 跳過前3條資料 使用skip應當避免跳過的資料過多

db.blog.fine().sort({age:-1) 按照age的值進行降序排列

mongo排列順序:

1.最小值,2.null 3.數字(整型,長整型,雙精度) 4.字串 5.物件/文件 6.陣列

7.二進位制資料 8.物件id 9.布林型 10.日期型 11.時間戳 12.正規表示式 13.最大值

本篇文件例子均出自《mongodb權威指南》

mongo聚合和mapreduce例子

聚合語句 比較集合內兩欄位大小 db.test.aggregate logtype off mac 1 1,0 聚合條數語句 db.tablename.aggregate logtype off mac 1 1,0 簡單聚合語句 db.test.aggregate buildingid 49,int...

mongo簡單操作

use admin 進入資料庫admin db.adduser name pwd 增加或修改使用者密碼 db.system.users.find 檢視使用者列表 db.auth name pwd 使用者認證 db.removeuser name 刪除使用者 show users 檢視所有使用者 sh...

mongo基礎操作

1.linux伺服器中已經安裝了mongo後,在mongo命令下的基礎shell命令 show dbs 顯示資料庫列表 show collections 顯示當前資料庫中的集合 類似關聯式資料庫中的表 show users 顯示使用者 use 切換當前資料庫,這和ms sql裡面的意思一樣 db.h...