MongoDB学习笔记(二)

一、Mongodb命令

说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法。  
对于database和collection无需主动创建,在插入数据时,如果database和collection不存在则会自动创建。

常用命令

help命令
通过此命令可以看到一些最基本的命令,如图:

use命令
例如命令【use demodb】,创建demodb,不用担心demodb不会创建,当使用use demodb 命令创建第一个collection时会自动创建数据库demodb,如图: 

插入数据
使用命令【db.collectionName.insert({name:"jack",age:33})】collectionName中插入一个document,如果collectionName不存在则创建。 使用命令【db.getCollectionNames()】会得到collectionName和system.indexex。system.indexex对于每个database都有,用于记录index。 使用命令【db.collectionName.find()】会查看到collectionName中的所有document。 命令如下:

复制代码
E:MongoDBin>mongo
MongoDB shell version: 2.6.5
connecting to: test
> use demodb
switched to db demodb
> db.FirstCollection.insert({name:"jack",age:22})
WriteResult({ "nInserted" : 1 })
> show collections
FirstCollection
system.indexes   
> db.getCollectionNames()
[ "FirstCollection", "system.indexes" ]
> db.demodb.find()
> db.FirstCollection.find()
{ "_id" : ObjectId("543731431dc491f307663a0d"), "name" : "jack", "age" : 22 }
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "demodb.FirstCollection" }
复制代码

查询数据

参考网址:http://www.cnblogs.com/stephen-liu74/archive/2012/08/03/2553803.html

MongoDB使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.  
find的第一个参数决定了要返回哪些文档(document的过滤条件).其形式也是一个文档,说明要查询的细节.空的查询文档{}会匹配集合的全部内容.  
要是不指定查询文档,默认是{}.如:db.users.find()返回集合中的所有内容.  
向查询文档中添加键值对,就意味着添加了查询条件.对绝大多数类型来说,整数匹配整  数,布尔类型匹配布尔类型,字符串匹配字符串.

先添加测试数据

db.Student.insert({name:"jack",sex:1,age:33})
db.Student.insert({name:"jack",sex:1,age:33})
db.Student.insert({name:"lily",sex:0,age:13})
db.Student.insert({name:"kaily",sex:0,age:33})
db.Student.insert({name:"tom",sex:1,age:53})

1、find()/findOne()条件过滤

只获取name等于jack的Student。findOne()则只获取第一条

复制代码
> use demodb
switched to db demodb
>  db.Student.find({name:"jack"})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age
" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age
" : 33 }
>  db.Student.findOne({name:"jack"})
{
"_id" : ObjectId("5437383157abafe09d99cbfc"),
"name" : "jack",
"sex" : 1,
"age" : 33
}
复制代码

2、find()/findOne()指定返回的fileds

说明:find()的第二个参数限制返回的filed的个数,0代表不返回,1代表返回。"_id"键总是会被返回。  
如果不带条件,只限制返回的filed个数的话,命令如下:db.Student.find({},{sex:0})。只需要第一个参数为{}空字典就可以。

 只获取name等于jack的Student,并且filed为name,age的数据。

复制代码
> db.Student.find({name:"jack"},{name:1,age:1})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "age" : 33 }
> db.Student.find({name:"jack"},{sex:0})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "age" : 33 }
>
复制代码

3、查询条件

"$lt","$lte","$gt","$gte"分别对应<,<=,>,>= 
如下代码:
db.Student.find({age:{$gt:33}}) 查询age大于33的 
db.Student.find({age:{$gte:33}})
db.Student.find({age:{$lt:33}})
db.Student.find({age:{$lte:33}})
db.Student.find({age:{$gt:23,$lt:43}})

复制代码
> db.Student.find({age:{$gt:33}})
{ "_id" : ObjectId("543738c357abafe09d99cc00"), "name" : "tom", "sex" : 1, "age" : 53 }
> db.Student.find({age:{$gte:33}})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }
{ "_id" : ObjectId("543738c357abafe09d99cc00"), "name" : "tom", "sex" : 1, "age" : 53 }
> db.Student.find({age:{$lt:33}})
{ "_id" : ObjectId("543738b857abafe09d99cbfe"), "name" : "lily", "sex" : 0, "age" : 13 }
> db.Student.find({age:{$lte:33}})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfe"), "name" : "lily", "sex" : 0, "age" : 13 }
{ "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }
> db.Student.find({age:{$gt:23,$lt:43}})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }  
复制代码

$ne 代表不等于 
db.Student.find({age:{$ne:33}}) 查询age不等于33
$in,$not和$or

复制代码
db.Student.find({age:{$in:[13,53]}})
db.Student.find(
{
$or:
   [
    {age:{$in:[13,53]}},
    {name:"kaily"}
   ]
}
)  
复制代码

4、特殊查询--null和exists

null可以匹配自身,而且可以匹配"不存在的"

--插入测试数据
db.Student.insert({name:null,sex:1,age:18})
db.Student.insert({sex:1,age:24})

db.Student.find({name:null})        --上面两条都能查到
db.Student.find({name:{$in:[null],$exists:true}})  ---只能查到第一条   

5、数组数据查询

db.Student.insert({name:"wjh",sex:1,age:18,color:["red","blue","black"]})
db.Student.insert({name:"lpj",sex:1,age:22,color:["white","blue","black"]})
db.Student.find()

--color数组中所有包含white的文档都会被检索出来
db.Student.find({color:"white"})
--color数组中所有包含red和blue的文档都会被检索出来,数组中必须同时包含red和blue,但是他们的顺序无关紧要。
db.Student.find({color:{$all:["red","blue"]}}) 
--精确匹配,即被检索出来的文档,color值中的数组数据必须和查询条件完全匹配,即不能多,也不能少,顺序也必须保持一致。
db.Student.find({color:["red","blue","black"]})
--匹配数组中指定下标元素的值。数组的起始下标是0。注意color要加引号。
db.Student.find({"color.0":"white"}) 

6、内嵌文档查询

----待完成-------   

7、排序

db.Student.find().sort({age:1})
db.Student.find().sort({age:1,sex:1})  

--1代表升序,-1代表降序

8、分页

db.Student.find().sort({age:1}).limit(3).skip(3) 

--limit代表取多少个document,skip代表跳过前多少个document。  

9、获取数量

db.Student.count({name:null})   
--或者
db.Student.find({name:null}).count()

删除数据

说明:删除数据比较简单。

db.Student.remove({name:null})

更新数据

1.更新数据

命令【db.Student.update({name:"jack"},{age:55})】执行后  
先查询name=jack的所有document,然后将name=jack的所有document都替换为{age:55},其它filed都没有了。  
正确的更新方式应该为:
db.Student.update({name:"jack"},{$set:{age:55}})  

2.增加field

--将name=lily的student增加一个filed height
db.Student.update({name:"lily"},{$inc:{height:175}})  

3.upset-将数字field增加多少增量

--若存在则添加,否则更新,只能用于数字field,例如age更新前是50,更新了185,则变为235.
db.Student.update({name:"lily"},{$inc:{age:185}},true)

4.批量更新数据

------------------待完成---------
原文地址:https://www.cnblogs.com/eer123/p/7077649.html