1.基本觀念:
MongoDB VS 關聯式資料庫(MySQL)database <-> database
collection <-> table
document <-> record
*不同的地方:資料長度可不同
2.使用方法
(1).建立 database:Blog
(2).建立 collection:Posts,Categories,Tags
(3).建立 document:Post:{"_id":"","title":""}
Blog
Posts
{"_id":1,"title":"Posts title01"}
{"_id":2,"title":"Posts title02"}
{"_id":3,"title":"Posts title03","delfg":1}
Categories
{"_id":1,"title":"Categories title01"}
{"_id":2,"title":"Categories title02"}
Tags
{"_id":1,"title":"Tags title01"}
{"_id":2,"title":"Tags title02"}
{"_id":3,"title":"Tags title03"}
2.指令:
操作實例:$mongo
> help
> exit
$mongo
>show dbs;
>use Blog
>db.createCollection("Posts")
>db.createCollection("Categories")
>db.createCollection("Tags")
>show collections
>show collections
3.Collection 實作
操作實例:
$mongo
>show dbs
>use Blog
>show collections
>db.createCollection("Posts")
>show collections
>db.Posts.renameCollection("Tags")
>show collections
>db.Tags.drop()
>show collections
>db.dropDataBase()
>show dbs
$mongo
>show dbs
>use Blog
>show collections
>db.createCollection("Posts")
>show collections
>db.Posts.renameCollection("Tags")
>show collections
>db.Tags.drop()
>show collections
>db.dropDataBase()
>show dbs
操作實例:
mongo
>show dbs
>use Blog
>show collections
>db.createCollection("Posts")
>db.Posts.insert({
title:"Posts title01",
content:"Posts content01"
})
>show collections
>db.Posts.find()
>db.Posts.insert({
title:"Posts title02",
content:"Posts content02",
tag:["Posts tag02"]
})
>db.Posts.find()
>for(var i = 3;i<=10;i++){
db.Posts.insert(
title:"Posts title0"+i
content:"Posts content0"+i
);
}
>db.Posts.find()
>db.Posts.count()
>db.Posts.remove({})
>db.Posts.count()
>db.Posts.find()
db.[collection_name].find({"":""})
$gte 大於等於 >=
$gt 大於 >
$eq 等於 =
$ne 不等於
正規表示法:/k/,/^k/
/k/ :帶有k的
/^k/ :不帶有k的
db.[collection_name].distinct("field_name")
操作實例:
mongo
>use Blog
>db.Posts.remove({})
>db.Posts.insert({title:Posts title01,"rank":2,"tag":"game"})
>db.Posts.insert({title:Posts title02,"rank":1,"tag":"game"})
>db.Posts.insert({title:Posts title03,"rank":3,"tag":"it"})
>db.Posts.insert({title:Posts title04,"rank":4,"tag":"game"})
>db.Posts.insert({title:Posts title05,"rank":7,"tag":"it"})
>db.Posts.insert({title:Posts title06,"rank":4,"tag":"game"})
>db.Posts.find({"tag":"game"})
>db.Posts.find({"rank":{$gte:4}})
>db.Posts.find({"rank":{$gt:4}})
>db.Posts.find({"rank":{$lte:4}})
>db.Posts.find({"rank":{$lt:4}})
>db.Posts.find({"title":/Posts/})
>db.Posts.distinct("tag")
mongo
>show dbs
>use Blog
>show collections
>db.createCollection("Posts")
>db.Posts.insert({
title:"Posts title01",
content:"Posts content01"
})
>show collections
>db.Posts.find()
>db.Posts.insert({
title:"Posts title02",
content:"Posts content02",
tag:["Posts tag02"]
})
>db.Posts.find()
>for(var i = 3;i<=10;i++){
db.Posts.insert(
title:"Posts title0"+i
content:"Posts content0"+i
);
}
>db.Posts.find()
>db.Posts.count()
>db.Posts.remove({})
>db.Posts.count()
>db.Posts.find()
5.限制條件的文檔db.find
觀念:db.[collection_name].find({"":""})
$gte 大於等於 >=
$gt 大於 >
$eq 等於 =
$ne 不等於
正規表示法:/k/,/^k/
/k/ :帶有k的
/^k/ :不帶有k的
db.[collection_name].distinct("field_name")
操作實例:
mongo
>use Blog
>db.Posts.remove({})
>db.Posts.insert({title:Posts title01,"rank":2,"tag":"game"})
>db.Posts.insert({title:Posts title02,"rank":1,"tag":"game"})
>db.Posts.insert({title:Posts title03,"rank":3,"tag":"it"})
>db.Posts.insert({title:Posts title04,"rank":4,"tag":"game"})
>db.Posts.insert({title:Posts title05,"rank":7,"tag":"it"})
>db.Posts.insert({title:Posts title06,"rank":4,"tag":"game"})
>db.Posts.find({"tag":"game"})
>db.Posts.find({"rank":{$gte:4}})
>db.Posts.find({"rank":{$gt:4}})
>db.Posts.find({"rank":{$lte:4}})
>db.Posts.find({"rank":{$lt:4}})
>db.Posts.find({"title":/Posts/})
>db.Posts.distinct("tag")
6.複雜條件:
觀念:
db.[collection_name].find({"":"","":""})
進行多個條件
db.[collection_name].find({$or:[{...},{...}]})
進行多個條件、[or]、[and]運算
db.[collection_name].find({"":{$in:[...]}})
進行關聯式資料庫的select_in
db.[collection_name].find({"":{$exists:true}})
進行[存在]就列出
進行多個條件
db.[collection_name].find({$or:[{...},{...}]})
進行多個條件、[or]、[and]運算
db.[collection_name].find({"":{$in:[...]}})
進行關聯式資料庫的select_in
db.[collection_name].find({"":{$exists:true}})
進行[存在]就列出
操作實例:
$mongo
>use Blog
>db.Posts.find()
>db.Posts.insert({"title":"Posts title09 u","istop":true})
>db.Posts.find({"title":/u/})
>db.Posts.find({$or:[{"title":/u/},{"rank":{$gte:4}}]})
>db.Posts.find({"rank":{$in:[3,4]}})
>db.Posts.insert({"title":"Posts title08","istop":true})
>db.Posts.find({"istop":{$exists:true}})
db.[collection_name].find({},{field1:true,field2:1})
列出field1是真的,field2:是真的文檔
操作實例:
$mongo
>use Blog
>db.Posts.find()
>db.Posts.find({},{title:true,rank:1})
>db.Posts.find({},{title:true,rank:1,_id:0})
隱藏_id
sort()
document的排序
limit() ex:limit(5)
document的限制列出 ex: 列出5個document
skip() ex:skjh(5)
document的跳過列出 ex跳過5個document
操作實例:
$mongo
>use Blog
>db.Posts.find()
>db.Posts.find({},{_id:0}).sort({rank:1})
rank:1 升冪排序
>db.Posts.find({},{_id:0}).sort({rank:-1})
rank:-1 降冪排序
>db.Posts.find({},{_id:0}).limit(3)
預設排序,前三個document
>db.Posts.find({},{_id:0}).sort({rank:-1}).limit(3)
>db.Posts.findOne({},{_id:0})
只要第一條document
>db.Posts.find({},{_id:0})
>db.Posts.find({},{_id:0}).limit(3)
>db.Posts.find({},{_id:0}).skip(3).limit(3)
update(<filter>,<update>,<options>)
操作實例:
$mongo
>use Blog
>db.Posts.findOne({title:"Posts title01"})
>db.Posts.update({title:"Posts title01"},{set:{"rank":10}})
>db.Posts.find()
資料來源:>use Blog
>db.Posts.find()
>db.Posts.insert({"title":"Posts title09 u","istop":true})
>db.Posts.find({"title":/u/})
>db.Posts.find({$or:[{"title":/u/},{"rank":{$gte:4}}]})
>db.Posts.find({"rank":{$in:[3,4]}})
>db.Posts.insert({"title":"Posts title08","istop":true})
>db.Posts.find({"istop":{$exists:true}})
7.列出指定條件下的document
觀念:db.[collection_name].find({},{field1:true,field2:1})
列出field1是真的,field2:是真的文檔
操作實例:
$mongo
>use Blog
>db.Posts.find()
>db.Posts.find({},{title:true,rank:1})
>db.Posts.find({},{title:true,rank:1,_id:0})
隱藏_id
8.document 的方法 sort limit skip
觀念:sort()
document的排序
limit() ex:limit(5)
document的限制列出 ex: 列出5個document
skip() ex:skjh(5)
document的跳過列出 ex跳過5個document
操作實例:
$mongo
>use Blog
>db.Posts.find()
>db.Posts.find({},{_id:0}).sort({rank:1})
rank:1 升冪排序
>db.Posts.find({},{_id:0}).sort({rank:-1})
rank:-1 降冪排序
>db.Posts.find({},{_id:0}).limit(3)
預設排序,前三個document
>db.Posts.find({},{_id:0}).sort({rank:-1}).limit(3)
>db.Posts.findOne({},{_id:0})
只要第一條document
>db.Posts.find({},{_id:0})
>db.Posts.find({},{_id:0}).limit(3)
>db.Posts.find({},{_id:0}).skip(3).limit(3)
9.document的更新update
觀念:update(<filter>,<update>,<options>)
操作實例:
$mongo
>use Blog
>db.Posts.findOne({title:"Posts title01"})
>db.Posts.update({title:"Posts title01"},{set:{"rank":10}})
>db.Posts.find()
1.MongoDB入門
2.DB-Engines Ranking
3.MongoDB學習
4.MongoDB官網
5.30天之你好MongoDB 系列
沒有留言:
張貼留言