MongoDB 查詢文檔使用 find() 方法。
find() 方法以非結構化的方式來顯示所有文檔。
mongodb的find().pretty()方法的作用。
使得查詢出來的數據在命令行中更加美觀的顯示,不至於太緊湊。
語法
MongoDB 查詢數據的語法格式如下:
db.collection.find(query, projection)
- query :可選,使用查詢操作符指定查詢條件
- projection :可選,使用投影操作符指定返回的鍵。查詢時返回文檔中所有鍵值, 只需省略該參數即可(默認省略)。
MongoDB 與 RDBMS Where 語句比較
如果你熟悉常規的 SQL 數據,通過下表可以更好的理解 MongoDB 的條件語句查詢:
| 操作 | 格式 | 范例 | RDBMS中的類似語句 |
|---|---|---|---|
| 等於 | {<key>:<value>} |
db.col.find({"by":"菜鳥教程"}).pretty() |
where by = '菜鳥教程' |
| 小於 | {<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
| 小於或等於 | {<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
| 大於 | {<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
| 大於或等於 | {<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
| 不等於 | {<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
MongoDB AND 條件
MongoDB 的 find() 方法可以傳入多個鍵(key),每個鍵(key)以逗號隔開,即常規 SQL 的 AND 條件。
語法格式如下:
>db.col.find({key1:value1, key2:value2}).pretty()

MongoDB OR 條件
MongoDB OR 條件語句使用了關鍵字 $or,語法格式如下:
>db.col.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()

AND 和 OR 聯合使用

日期查詢


MongoDB 日期查詢目前可通過Date 和ISODate兩種方式: 1.Date方式,2.ISODate方式,查詢大於等於北京時間2013年2月25日0點的數據記錄數方法為,MongoDB中的格式是標准時間,相差8小時:

MongoDB的面向文檔(document-oriented)設計中采用更為靈活的文檔來作為數據模型用來取代RDBMS中的行,面向文檔的設計讓開發人員獲取數據的方式更加靈活,甚至於開發人員僅用一條語句即可查詢復雜的嵌套關系,讓開發人員不必為了獲取數據而絞盡腦汁。它可以是實現高並發,適用於大數據領域。
傳統數據庫設計思維中,項目的設計階段需要對數據庫表中的字段名稱、字段類型、進行規定,如果嘗試插入不符合設計的數據,數據庫不會接受這條數據以保證數據的完整性。
NoSQL采用的是對集合(類似"表")中的文檔(類似於"行")進行動態追加,在創建集合之初不會對數據類型進行限定,任何文檔都可以追加到任何集合中去.,但種類很多的字段單獨設計一張表,可以將他們集中在單獨一張表進行存儲,但這樣做的弊病也是顯而易見的,我們在獲取數據時需要對同一張表的不同文檔進行區分,增加了開發上的代碼量。所以在設計之初需要權衡動態模式的優劣來選擇表中的數據類型。
MongoDB不支持事務,現在眾多的軟件依舊需要事務的管理,所以對於事務一致性要求較高的程序只能在軟件層面進行管理,而無法從數據庫進行管理。
database ==> 數據庫;
collection ==> 集合(表);
document ==> 文檔(數據記錄);
field ==> 域(數據字段);
index ==> 索引;
primary key ==> 主鍵,MongoDB自動將_id字段設置為主鍵。
創建數據庫
use databasename
如:
> db.getName()
test
> use mongotest
switched to db mongotest
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
> db
mongotest
可以看到,我們剛創建的數據庫 mongotest並不在數據庫的列表中, 要顯示它,我們需要向 mongotest數據庫插入一些數據。
> db.mongotest.insert({"id":10001,"name":"tianyongtao","regtime":Date()})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mongotest 0.000GB
刪除數據庫
> use test510
switched to db test510
> db.test510.insert({"id":222,"name":"tianyongtao"})
WriteResult({ "nInserted" : 1 })
> db.test510
test510.test510
> db.dropDatabase()
{ "dropped" : "test510", "ok" : 1 }
-------------------------------------------------
添加數據集刪除數據集
> use mongo510
switched to db mongo510
> db.mongo510.insert({"id":001,"name":"tianyt"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mongo510 0.000GB
MongoDB 中使用 createCollection() 方法來創建集合。
語法格式:db.createCollection(name, options) 參數說明: name: 要創建的集合名稱 options: 可選參數, 指定有關內存大小及索引的選項、
> db.createCollection("userinfo")
{ "ok" : 1 }
> db
mongo510
> show collections
mongo510
userinfo
在 MongoDB 中,你不需要創建集合。當你插入一些文檔時,MongoDB 會自動創建集合。
> db.createCollection("recharge")
{ "ok" : 1 }
> show collections
mongo510
recharge
userinfo
> db.recharge.drop()
true
> show collections
mongo510
userinfo
文檔的數據結構和 JSON 基本一樣。
所有存儲在集合中的數據都是 BSON 格式。
BSON 是一種類似 JSON 的二進制形式的存儲格式,是 Binary JSON 的簡稱。
> db.userinfo.insert({"id":001,"name":"liu"})
WriteResult({ "nInserted" : 1 })
> db.userinfo.insert({"id":001,"name":"liu","regdate":"2019-05-10 15:35"})
WriteResult({ "nInserted" : 1 })
> db.userinfo.find()
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "id" : 1, "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
> db.userinfo.find({},{"name":1,"regdate":1})
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "name" : "liu", "regdate" : "2019-05-10 15:35" }
> db.userinfo.find({},{"name":1,"regdate":1,_id:false})
{ "name" : "liu" }
{ "name" : "liu", "regdate" : "2019-05-10 15:35" }
> db.userinfo.find({},{"name":1,"regdate":1,_id:0})
{ "name" : "liu" }
{ "name" : "liu", "regdate" : "2019-05-10 15:35" }
也可以將數據定義為一個變量,然后將文檔插入
> doc510={id:56,name:"ma",regdate:"2019-05-10 18:01"}
{ "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
> db.userinfo.insert(doc510)
WriteResult({ "nInserted" : 1 })
> db.userinfo.find()
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "id" : 1, "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
> db.userinfo.find({"id":{$gt:50}})
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
插入文檔你也可以使用 db.col.save(document) 命令。如果不指定 _id 字段 save() 方法類似於 insert() 方法。如果指定 _id 字段,則會更新該 _id 的數據。
- db.collection.insertOne():向指定集合中插入一條文檔數據
- db.collection.insertMany():向指定集合中插入多條文檔數據
> db.userinfo.insertOne(doc5101)
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd552af02d3c2a6078c8c52")
}
> db.userinfo.insertOne(doc5102)
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd552b602d3c2a6078c8c53")
}
> db.userinfo.find()
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "id" : 1, "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5526d02d3c2a6078c8c51"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552af02d3c2a6078c8c52"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552b602d3c2a6078c8c53"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
> db.userinfo.insertMany([doc5101,doc5102])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5cd552e002d3c2a6078c8c54"),
ObjectId("5cd552e002d3c2a6078c8c55")
]
}
> db.userinfo.find()
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "id" : 1, "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5526d02d3c2a6078c8c51"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552af02d3c2a6078c8c52"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552b602d3c2a6078c8c53"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
{ "_id" : ObjectId("5cd552e002d3c2a6078c8c54"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552e002d3c2a6078c8c55"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
一次插入多條數據
1、先創建數組
2、將數據放在數組中
3、一次 insert 到集合中
> var arr=[]
> for(var i=1;i<10;i++){
... arr.push({id:i,name:i})
... }
9
> db.userinfo.find()
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "id" : 1, "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5526d02d3c2a6078c8c51"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552af02d3c2a6078c8c52"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552b602d3c2a6078c8c53"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
{ "_id" : ObjectId("5cd552e002d3c2a6078c8c54"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552e002d3c2a6078c8c55"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c56"), "id" : 1, "name" : 1 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c58"), "id" : 3, "name" : 3 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c59"), "id" : 4, "name" : 4 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5a"), "id" : 5, "name" : 5 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : 6 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : 7 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : 8 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
id大於5小於等於9,並且name在6,9中的數據
> db.userinfo.find({id:{$gt:5,$lte:9},"name":{$in:[6,9]}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : 6 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
MongoDB 使用 update() 和 save() 方法來更新集合中的文檔。
update() 方法
update() 方法用於更新已存在的文檔。語法格式如下:
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
參數說明:
- query : update的查詢條件,類似sql update查詢內where后面的。
- update : update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內set后面的
- upsert : 可選,這個參數的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認是false,不插入。
- multi : 可選,mongodb 默認是false,只更新找到的第一條記錄,如果這個參數為true,就把按條件查出來多條記錄全部更新。
- writeConcern :可選,拋出異常的級別。
> db.userinfo.update({id:{$gt:5,$lte:9},"name":{$in:[6,9]}},{$set:{"name":"tian"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.userinfo.find({id:{$gt:5,$lte:9},"name":{$in:[6,9]}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
> db.userinfo.find()
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "id" : 1, "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5526d02d3c2a6078c8c51"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552af02d3c2a6078c8c52"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552b602d3c2a6078c8c53"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
{ "_id" : ObjectId("5cd552e002d3c2a6078c8c54"), "id" : 560, "name" : "zhou", "regdate" : "2019-05-10 18:06" }
{ "_id" : ObjectId("5cd552e002d3c2a6078c8c55"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c56"), "id" : 1, "name" : 1 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c58"), "id" : 3, "name" : 3 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c59"), "id" : 4, "name" : 4 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5a"), "id" : 5, "name" : 5 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : "tian" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : 7 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : 8 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
> db.userinfo.update({id:{$gt:5,$lte:9},"name":{$in:[7,8]}},{$set:{"name":"tianhaha"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.userinfo.find({id:{$gt:5,$lte:9},"id":{$in:[7,8]}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "tianhaha" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "tianhaha" }
> db.userinfo.update({id:{$gt:5,$lte:9},"id":{$in:[7,8]}},{$set:{"name":"duguqiubai"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.userinfo.find({id:{$gt:5,$lte:9},"id":{$in:[7,8]}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "duguqiubai" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "duguqiubai" }
> db.userinfo.update({id:{$gt:5,$lte:9},"id":{$in:[7,8]}},{$set:{"name":"dongfangbubai"}},true,true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.userinfo.find({id:{$gt:5,$lte:9},"id":{$in:[7,8]}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "dongfangbubai" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "dongfangbubai" }
> db.userinfo.update({id:{$gt:5,$lte:9},"id":{$in:[7,8]}},{$set:{"name":"fengqingyang"}},false,true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.userinfo.find({id:{$gt:5,$lte:9},"id":{$in:[7,8]}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "fengqingyang" }
只更新第一條記錄:
全部更新:
只添加第一條:
全部添加進去:
全部更新:
只更新第一條記錄:
save() 方法
save() 方法通過傳入的文檔來替換已有文檔。語法格式如下:
db.collection.save( <document>, { writeConcern: <document> } )
參數說明:
- document : 文檔數據。
- writeConcern :可選,拋出異常的級別。
刪除文檔
db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> } )
參數說明:
- query :(可選)刪除的文檔的條件。
- justOne : (可選)如果設為 true 或 1,則只刪除一個文檔,如果不設置該參數,或使用默認值 false,則刪除所有匹配條件的文檔。
- writeConcern :(可選)拋出異常的級別。
> db.userinfo.find({id:{$lte:1}})
{ "_id" : ObjectId("5cd54d7502d3c2a6078c8c4e"), "id" : 1, "name" : "liu" }
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c56"), "id" : 1, "name" : 1 }
> db.userinfo.remove({id:{$lte:1}},true)
WriteResult({ "nRemoved" : 1 })
> db.userinfo.find({id:{$lte:1}})
{ "_id" : ObjectId("5cd54dc902d3c2a6078c8c4f"), "id" : 1, "name" : "liu", "regdate" : "2019-05-10 15:35" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c56"), "id" : 1, "name" : 1 }
> db.userinfo.remove({id:{$lte:1}},false)
WriteResult({ "nRemoved" : 2 })
> db.userinfo.find({id:{$gte:561}})
{ "_id" : ObjectId("5cd552b602d3c2a6078c8c53"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
{ "_id" : ObjectId("5cd552e002d3c2a6078c8c55"), "id" : 561, "name" : "wang", "regdate" : "2019-05-10 18:08" }
> db.userinfo.remove({id:{$lte:1}},1)
WriteResult({ "nRemoved" : 0 })
> db.userinfo.remove({id:{$gte:561}},1)
WriteResult({ "nRemoved" : 1 })
> db.userinfo.remove({id:{$gte:561}},0)
WriteResult({ "nRemoved" : 1 })
> db.userinfo.remove({id:{$gte:560}},0)
WriteResult({ "nRemoved" : 3 })
語法
MongoDB 查詢數據的語法格式如下:
db.collection.find(query, projection)
- query :可選,使用查詢操作符指定查詢條件
- projection :可選,使用投影操作符指定返回的鍵。查詢時返回文檔中所有鍵值, 只需省略該參數即可(默認省略)。
如果你需要以易讀的方式來讀取數據,可以使用 pretty() 方法,語法格式如下:
>db.col.find().pretty()
pretty() 方法以格式化的方式來顯示所有文檔
模糊查詢
查詢 title 包含"教"字的文檔:
db.col.find({title:/教/})
查詢 title 字段以"教"字開頭的文檔:
db.col.find({title:/^教/})
查詢 titl e字段以"教"字結尾的文檔:
db.col.find({title:/教$/})
$type操作符是基於BSON類型來檢索集合中匹配的數據類型,並返回結果。
> db.userinfo.find({"name":{$type:2}})
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : "tian" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "fengqingyang" }
> db.userinfo.find({"name":{$type:1}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c58"), "id" : 3, "name" : 3 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c59"), "id" : 4, "name" : 4 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5a"), "id" : 5, "name" : 5 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
> db.userinfo.find({"name":{$type:'string'}})
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : "tian" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "fengqingyang" }
> db.userinfo.find({"name":{$type:'double'}})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c58"), "id" : 3, "name" : 3 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c59"), "id" : 4, "name" : 4 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5a"), "id" : 5, "name" : 5 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
> db.userinfo.find().limit(2)
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
MongoDB Skip() 方法
我們除了可以使用limit()方法來讀取指定數量的數據外,還可以使用skip()方法來跳過指定數量的數據,skip方法同樣接受一個數字參數作為跳過的記錄條數。
語法
skip() 方法腳本語法格式如下:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
MongoDB sort() 方法
在 MongoDB 中使用 sort() 方法對數據進行排序,sort() 方法可以通過參數指定排序的字段,並使用 1 和 -1 來指定排序的方式,其中 1 為升序排列,而 -1 是用於降序排列。
語法
sort()方法基本語法如下所示:
>db.COLLECTION_NAME.find().sort({KEY:1})
> db.userinfo.find().sort({"id":1})
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c58"), "id" : 3, "name" : 3 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c59"), "id" : 4, "name" : 4 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5a"), "id" : 5, "name" : 5 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : "tian" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
> db.userinfo.find().sort({"id":-1})
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma", "regdate" : "2019-05-10 18:01" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : "tian" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5a"), "id" : 5, "name" : 5 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c59"), "id" : 4, "name" : 4 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c58"), "id" : 3, "name" : 3 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
MongoDB使用 createIndex() 方法來創建索引。
語法
createIndex()方法基本語法格式如下所示:
>db.collection.createIndex(keys, options)
語法中 Key 值為你要創建的索引字段,1 為指定按升序創建索引,如果你想按降序來創建索引指定為 -1 即可。
1、查看集合索引
db.col.getIndexes()
2、查看集合索引大小
db.col.totalIndexSize()
3、刪除集合所有索引
db.col.dropIndexes()
4、刪除集合指定索引
db.col.dropIndex("索引名稱")
> db.userinfo.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mongo510.userinfo"
}
]
> db.userinfo.createIndex({"id":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.userinfo.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mongo510.userinfo"
},
{
"v" : 2,
"key" : {
"id" : 1
},
"name" : "id_1",
"ns" : "mongo510.userinfo"
}
]
> db.userinfo.dropIndexes()
{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
> db.userinfo.find({},{"_id":0,"id":1,"name":2})
{ "id" : 56, "name" : "ma" }
{ "id" : 2, "name" : 2 }
{ "id" : 3, "name" : 3 }
{ "id" : 4, "name" : 4 }
{ "id" : 5, "name" : 5 }
{ "id" : 6, "name" : "tian" }
{ "id" : 7, "name" : "fengqingyang" }
{ "id" : 8, "name" : "fengqingyang" }
{ "id" : 9, "name" : 9 }
> db.userinfo.find({},{"_id":false,"id":1,"name":2})
{ "id" : 56, "name" : "ma" }
{ "id" : 2, "name" : 2 }
{ "id" : 3, "name" : 3 }
{ "id" : 4, "name" : 4 }
{ "id" : 5, "name" : 5 }
{ "id" : 6, "name" : "tian" }
{ "id" : 7, "name" : "fengqingyang" }
{ "id" : 8, "name" : "fengqingyang" }
{ "id" : 9, "name" : 9 }
> db.userinfo.find({},{"id":1,"name":2})
{ "_id" : ObjectId("5cd5502202d3c2a6078c8c50"), "id" : 56, "name" : "ma" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c57"), "id" : 2, "name" : 2 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c58"), "id" : 3, "name" : 3 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c59"), "id" : 4, "name" : 4 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5a"), "id" : 5, "name" : 5 }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5b"), "id" : 6, "name" : "tian" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5c"), "id" : 7, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5d"), "id" : 8, "name" : "fengqingyang" }
{ "_id" : ObjectId("5cd5550f02d3c2a6078c8c5e"), "id" : 9, "name" : 9 }
MongoDB 聚合
MongoDB中聚合(aggregate)主要用於處理數據(諸如統計平均值,求和等),並返回計算后的數據結果。有點類似sql語句中的 count(*)。
aggregate() 方法
MongoDB中聚合的方法使用aggregate()。
語法
aggregate() 方法的基本語法格式如下所示:
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
管道的概念
管道在Unix和Linux中一般用於將當前命令的輸出結果作為下一個命令的參數。
MongoDB的聚合管道將MongoDB文檔在一個管道處理完畢后將結果傳遞給下一個管道處理。管道操作是可以重復的。
表達式:處理輸入文檔並輸出。表達式是無狀態的,只能用於計算當前聚合管道的文檔,不能處理其它的文檔。
這里我們介紹一下聚合框架中常用的幾個操作:
- $project:修改輸入文檔的結構。可以用來重命名、增加或刪除域,也可以用於創建計算結果以及嵌套文檔。
- $match:用於過濾數據,只輸出符合條件的文檔。$match使用MongoDB的標准查詢操作。
- $limit:用來限制MongoDB聚合管道返回的文檔數。
- $skip:在聚合管道中跳過指定數量的文檔,並返回余下的文檔。
- $unwind:將文檔中的某一個數組類型字段拆分成多條,每條包含數組中的一個值。
- $group:將集合中的文檔分組,可用於統計結果。
- $sort:將輸入文檔排序后輸出。
- $geoNear:輸出接近某一地理位置的有序文檔。
例子:id<50后按id分組求數量
> db.userinfo.aggregate([{$match:{"id":{$lt:50}}},{$group:{"_id":{id:"$id"},count:{$sum:1}}}])
{ "_id" : { "id" : 9 }, "count" : 1 }
{ "_id" : { "id" : 8 }, "count" : 1 }
{ "_id" : { "id" : 7 }, "count" : 1 }
{ "_id" : { "id" : 6 }, "count" : 1 }
{ "_id" : { "id" : 2 }, "count" : 1 }
{ "_id" : { "id" : 5 }, "count" : 3 }
{ "_id" : { "id" : 3 }, "count" : 2 }
{ "_id" : { "id" : 4 }, "count" : 1 }
> db.userinfo.aggregate([{$match:{"id":{$lt:50}}},{$group:{"_id":{id:"$id"},count:{$sum:1}}},{$project:{"_id":0,"id":"$_id.id","count":"$count"}}])
{ "id" : 9, "count" : 1 }
{ "id" : 8, "count" : 1 }
{ "id" : 7, "count" : 1 }
{ "id" : 6, "count" : 1 }
{ "id" : 2, "count" : 1 }
{ "id" : 5, "count" : 3 }
{ "id" : 3, "count" : 2 }
{ "id" : 4, "count" : 1 }
相當於sql:select id,count(1) as count from userinfo where id<50 group by id;
> db.userinfo.aggregate([{$match:{"id":{$lt:50}}},{$group:{"_id":{id:"$id"},arr:{$push:"$name"}}}])
{ "_id" : { "id" : 9 }, "arr" : [ 9 ] }
{ "_id" : { "id" : 8 }, "arr" : [ "fengqingyang" ] }
{ "_id" : { "id" : 7 }, "arr" : [ "fengqingyang" ] }
{ "_id" : { "id" : 6 }, "arr" : [ "tian" ] }
{ "_id" : { "id" : 2 }, "arr" : [ 2 ] }
{ "_id" : { "id" : 5 }, "arr" : [ 5, "liu", "xiaoma" ] }
{ "_id" : { "id" : 3 }, "arr" : [ 3, "dan" ] }
{ "_id" : { "id" : 4 }, "arr" : [ 4 ] }
> db.userinfo.aggregate([{$match:{"id":{$lt:50}}},{$group:{"_id":"$id",arr:{$push:"$name"}}}])
{ "_id" : 9, "arr" : [ 9 ] }
{ "_id" : 8, "arr" : [ "fengqingyang" ] }
{ "_id" : 7, "arr" : [ "fengqingyang" ] }
{ "_id" : 6, "arr" : [ "tian" ] }
{ "_id" : 2, "arr" : [ 2 ] }
{ "_id" : 5, "arr" : [ 5, "liu", "xiaoma" ] }
{ "_id" : 3, "arr" : [ 3, "dan" ] }
{ "_id" : 4, "arr" : [ 4 ] }
> db.userinfo.aggregate([{$match:{"id":{$lt:50}}},{$group:{"_id":"$id",arr:{$addToSet:"$name"}}}])
{ "_id" : 9, "arr" : [ 9 ] }
{ "_id" : 8, "arr" : [ "fengqingyang" ] }
{ "_id" : 7, "arr" : [ "fengqingyang" ] }
{ "_id" : 6, "arr" : [ "tian" ] }
{ "_id" : 2, "arr" : [ 2 ] }
{ "_id" : 5, "arr" : [ "xiaoma", "liu", 5 ] }
{ "_id" : 3, "arr" : [ "dan", 3 ] }
{ "_id" : 4, "arr" : [ 4 ] }
