基本概念
- Document
MongoDB中數(shù)據(jù)的基本單元遭商,非常類似于關(guān)系型數(shù)據(jù)系統(tǒng)中的行 - Collection
可以看成一個(gè)擁有動(dòng)態(tài)模式的表览绿,一個(gè)Collection里面的Document可以是各式各樣的 - Database
一個(gè)MongoDB實(shí)例可以擁有多個(gè)相互獨(dú)立的Database茉盏,每一個(gè)Database都擁有自己的集合 - Mongo Shell
MongoDB自帶的一個(gè)簡(jiǎn)單但功能強(qiáng)大的JavaScript shell确垫,可用于管理MongoDB的實(shí)例或數(shù)據(jù)操作
shell CRUD操作
創(chuàng)建
> use test
switched to db test
> post ={"title":"My Blog Post",
... "content":"Here's my blog post.",
... "date":new Date()
... }
{
"title" : "My Blog Post",
"content" : "Here's my blog post.",
"date" : ISODate("2017-05-22T07:29:26.085Z")
}
//post是一個(gè)有效的文檔樱蛤,下面將其插入數(shù)據(jù)庫
> db.blog.insert(post)
WriteResult({ "nInserted" : 1 })
//查詢blog Collection,發(fā)現(xiàn)blog文檔已經(jīng)插入
> db.blog.find()
{ "_id" : ObjectId("5922935f01d2563bad1ad3f8"), "title" : "My Blog Post", "content" : "Here's my blog post.", "date" : ISODate("2017-05-22T07:29:26.085Z") }
>
讀取
find和findOne可以接受一個(gè)查詢文檔作為限定文件翅帜。findOne代表只查看一個(gè)文檔。
> db.blog.findOne()
{
"_id" : ObjectId("5922935f01d2563bad1ad3f8"),
"title" : "My Blog Post",
"content" : "Here's my blog post.",
"date" : ISODate("2017-05-22T07:29:26.085Z")
}
更新
> post.comments = []
[ ]
> db.blog.update({"title" : "My Blog Post"},post)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.find()
{ "_id" : ObjectId("5922935f01d2563bad1ad3f8"), "title" : "My Blog Post", "content" : "Here's my blog post.", "date" : ISODate("2017-05-22T07:29:26.085Z"), "comments" : [ ] }
update接受(至少)兩個(gè)參數(shù)命满,第一個(gè)是限定條件(用于匹配待更新的文檔)涝滴,第二個(gè)是新的文檔
刪除
> db.blog.remove({"title" : "My Blog Post"})
WriteResult({ "nRemoved" : 1 })
> db.blog.find()
>
remove可將數(shù)據(jù)庫中的文檔永久刪除。如果沒有傳入?yún)?shù)胶台,將會(huì)刪除指定Collection中的全部文檔歼疮,可以接受一個(gè)作為限定條件的文檔為參數(shù)。
查詢
//指定需要返回的鍵
> db.blog.find({},{"username":1})
{ "_id" : ObjectId("5922c9fb01d2563bad1ad3fb"), "username" : "Joe" }
//剔除查詢結(jié)果中某些鍵
> db.blog.find({},{"username":0,"_id":0})
{ "email" : "joe@example.com", "address" : "Beijing" }
//查詢條件 "$lt" "$lte" "$gt" "gte"
> db.users.find({"registered":{"$lt":new Date("01/01/2018")}})
{ "_id" : ObjectId("5922cb0601d2563bad1ad3fc"), "name" : "test", "email" : "test@example.com", "age" : 30, "registered" : ISODate("2017-05-22T11:26:45.167Z") }
//OR查詢 "$in" "$or"
> db.raffle.find({"ticket_no":{"$in":[725,453,380]}})
> db.raffle.find({"$or":[{"ticket_no":735},{"winner":true}]})
// $not
> db.users.find({"id_num":{"$not":{"$mod":[5,1]}}})
//特定類型查詢 null
> db.c.find({"y":null}) //此時(shí)返回了不含y鍵的document
{ "_id" : ObjectId("5922cdfd01d2563bad1ad3ff"), "y" : null }
{ "_id" : ObjectId("5922ce1e01d2563bad1ad400"), "z" : 1 }
> db.c.find({"y":{"$in":[null],"$exists":true}}) //因?yàn)闆]有$eq,采用$in操作符
{ "_id" : ObjectId("5922cdfd01d2563bad1ad3ff"), "y" : null }
//正則
> db.users.find({"name":/Test/i})
{ "_id" : ObjectId("5922cb0601d2563bad1ad3fc"), "name" : "test", "email" : "test@example.com", "age" : 30, "registered" : ISODate("2017-05-22T11:26:45.167Z") }
//查詢數(shù)組(與查詢標(biāo)量值一樣)
> db.food.insert({"fruit":["apple","banana"]})
WriteResult({ "nInserted" : 1 })
> db.food.find({"fruit":"banana"})
{ "_id" : ObjectId("5922cf2d01d2563bad1ad401"), "fruit" : [ "apple", "banana" ] }
//查詢數(shù)組 $all
> db.food.find({"fruit":{$all:["banana","apple"]}}) //$all對(duì)應(yīng)數(shù)組诈唬,順序無關(guān)
{ "_id" : ObjectId("5922cf2d01d2563bad1ad401"), "fruit" : [ "apple", "banana" ] }
//精確匹配
> db.food.find()
{ "_id" : ObjectId("5922cf2d01d2563bad1ad401"), "fruit" : [ "apple", "banana" ] }
> db.food.find({"fruit":["banana","apple"]})
//查詢數(shù)組 $size
> db.food.find({"fruit":{"$size":2}})
{ "_id" : ObjectId("5922cf2d01d2563bad1ad401"), "fruit" : [ "apple", "banana" ] }
//查詢數(shù)組 $slice
> db.blog.posts.find()
{ "_id" : ObjectId("5922d14801d2563bad1ad402"), "comments" : [ "d", "dadfs", "ddd", "good" ], "title" : "Nice Title" }
{ "_id" : ObjectId("5922d1d501d2563bad1ad403"), "criteria" : { "comments" : [ "d", "dadfs", "ddd", "good" ] } }
> db.blog.posts.findOne({},{"comments":{"$slice":2}}) //find第二個(gè)參數(shù)可選(可以指定需要返回的鍵)韩脏,通過$slice操作可以返回某個(gè)鍵匹配的數(shù)組元素子集
{
"_id" : ObjectId("5922d14801d2563bad1ad402"),
"comments" : [
"d",
"dadfs"
],
"title" : "Nice Title"
}
//指定數(shù)組區(qū)間
> db.blog.posts.findOne({},{"comments":{"$slice":[1,3]}})
{
"_id" : ObjectId("5922d14801d2563bad1ad402"),
"comments" : [
"dadfs",
"ddd",
"good"
],
"title" : "Nice Title"
//查詢內(nèi)嵌文檔
{
"content" : "dfadsf",
"comments" : [
{
"author" : "joe",
"score" : 3,
"comment" : "nice post"
},
{
"author" : "mary",
"score" : 5,
"comment" : "terrible post"
}
]
}
> db.blog.insert(a)
WriteResult({ "nInserted" : 1 })
> db.blog.find({"comments":{"author":"joe","score":{"$gte":5}}}) //匹配為空,因?yàn)閮?nèi)嵌文檔的匹配铸磅,必須要整個(gè)文檔完全匹配
> db.blog.find({"comments":{"author":"joe","score":3,"comment":"nice post"}})
{ "_id" : ObjectId("5922d3d001d2563bad1ad404"), "content" : "dfadsf", "comments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "mary", "score" : 5, "comment" : "terrible post" } ] }
//使用$eleMatch匹配內(nèi)嵌文檔
> db.blog.find({"comments":{"$elemMatch":{"author":"joe","score":{"$gte":1}}}})
{ "_id" : ObjectId("5922d3d001d2563bad1ad404"), "content" : "dfadsf", "comments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "mary", "score" : 5, "comment" : "terrible post" } ] }
//limit skip sort
db.c.find().limit(3) //返回?cái)?shù)量上限
db.c.find().skip(3)
db.c.find().sort({username:1,age:-1}) //1升序 -1降序