《左手MongoDB右手Redis》第3章笔记-robo3t上进行增删改查
表3-1 SQL與MongoDB術(shù)語對(duì)比
無論是插入一條數(shù)據(jù)還是插入多條數(shù)據(jù),每一條數(shù)據(jù)被插入
MongoDB 后都會(huì)被自動(dòng)添加一個(gè)字段“_id”。“_id”讀作“Object Id”,它
是由時(shí)間、機(jī)器碼、進(jìn)程pid和自增計(jì)數(shù)器構(gòu)成的。
“_id”始終遞增,但絕不重復(fù)。
● 同一時(shí)間,不同機(jī)器上面的“_id”不同。
● 同一機(jī)器,不同時(shí)間的“_id”也不同。
● 同一機(jī)器同一時(shí)間批量插入的數(shù)據(jù),“_id”依然不同。
#---------------------------------------------robo3t操作-------------------------------------
robo3t創(chuàng)建數(shù)據(jù)庫:
?
創(chuàng)建集合:
?
插入單條數(shù)據(jù):
db.getCollection('example_data_1').insertOne({"name": "張小二 ", "age": 17,"address": "浙江"})
JSON字符串必須使用雙引號(hào),不過這個(gè)規(guī)定在MongoDB中并非強(qiáng)制性的,用單引號(hào)也沒有問題。
db.getCollection("example_data_1").insertOne({"name":"王小六", "age": 25, "work":"廚師"})
插入后效果如下:
?
db.getCollection("example_data_1").insertOne({"hello":"world","sex":"男","職位":"工程師"})
db.getCollection("example_data_1").insertOne({"name":18030,"age":"十歲","address":3.5})
db.getCollection("example_data_1").insertMany([
{"name":"朱小三","age":20,"address":"北京"},
{"name":"劉小四","age":21,"address":"上海"},
{"name":"馬小五","age":22,"address":"山東"},
{"name":"夏侯小七","age":23,"address":"河北"},
{"name":"公孫小八","age":24,"address":"廣州"},
{"name":"慕容小久","age":25,"address":"杭州"},
{"name":"歐陽小十","age":26,"address":"深圳"},
])
?
| 任務(wù) | mongodb 代碼 |
| 查詢所有數(shù)據(jù) | db.getCollection('example_data_1').find({'age': 25}) |
| 查詢特定數(shù)據(jù) | db.getCollection("example_data_1").find({"age":25,"name":"慕容小九"}) |
| 查詢范圍值數(shù)據(jù) | db.getCollection('example_data_1').find({'age': {'$gte': 25}}) |
| 查詢所有“age”大于21并小于等于24的數(shù)據(jù)。 | db.getCollection('example_data_1').find({'age': {'$lt': 25, '$gt': 21}}) |
| 查詢所有“age”大于21并小于等于24的數(shù)據(jù),且“name”不 為“夏侯小七”的記錄 | db.getCollection("example_data_1").find({ "age":{ "$lt":25, "$gt":21, }, "name":{"$ne":"夏侯小七"}}) |
| 限定返回哪些字段 | db.getCollection('example_data_1').find({}, {'address': 0, 'age': 0}) |
| 滿足要求的數(shù)據(jù)有多少條 | db.getCollection('example_data_1').find({'age': {'$gt': 21}}).count() |
| 限定返回結(jié)果——“l(fā)imit()”命令 | db.getCollection('example_data_1').find().limit(4) |
| 對(duì)查詢結(jié)果進(jìn)行排序——“sort()”命令 | db.getCollection('example_data_1').find({'age': {'$gt': 21}}).sort({'age': -1}) |
| 更新集合中的單條數(shù)據(jù) | db.getCollection("example_data_1").updateMany( {"name":"王小六"}, {"$set":{"address":"蘇州","work":"工程師"}} ) |
| 查詢字段“hello”中值為“world”的這一條記錄 | db.getCollection('example_data_1').find({'hello': 'world'}) |
| 刪 除所有滿足要求的數(shù)據(jù) | db.getCollection('example_data_1').deleteMany({'hello': 'world'}) |
| 對(duì)“age”字段去重 | db.getCollection('example_data_1').distinct('age') |
| 對(duì)“age”大于等于24的記錄的“age”字段去重 | db.getCollection("example_data_1").distinct( "age", {"age":{"$gte":24}} ) |
慎用刪除功能。一般工程上會(huì)使用“假刪除”,即:在文檔里面
增加一個(gè)字段“deleted”,如果值為0則表示沒有刪除,如果值為1則
表示已經(jīng)被刪除了。
默認(rèn)情況下,deleted字段的值都是0,如需要執(zhí)行刪除操作,則
把這個(gè)字段的值更新為1。而查詢數(shù)據(jù)時(shí),只查詢deleted為0的數(shù)據(jù)。
這樣就實(shí)現(xiàn)了和刪除一樣的效果,即使誤操作了也可以輕易恢復(fù)
?
總結(jié)
以上是生活随笔為你收集整理的《左手MongoDB右手Redis》第3章笔记-robo3t上进行增删改查的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 番茄小说app如何手动对换钱(好看的小说
- 下一篇: mysql的事务操作