Mongodb操作详解(二)
Mongodb操作详解(二)
> db.blog.find(null,{title:1}) //只查询title字段
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "title" : "My Blog Post" }
> db.blog.find(null,{title:1,_id:0}) //只查询title字段,并过滤掉_id字段
{ "title" : "My Blog Post" } www.zzzyk.com
> db.blog.find().sort({title:1}) //sort 以title字段升序查询--- 1表示升序,-1表示降序
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "title" : "My Blog Post", "conte
nt" : "Here's my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "
comments" : [ ] }
> db.blog.find().sort({title:1}).limit(2).skip(1) //隔一个,查询两个
> db.blog.find().count() //count集合函数
1
> db.blog.update({title:'My Blog Post'},{$set:{content:'Here is my blog post.'}}
//使用$set只更新content的内容,不然整个数据变为content字段值
> db.blog.find()
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "title"
: "My Blog Post" }
> db.blog.update({title:'My Blog Post'},{$inc:{number:1}}) //number字段不存在,自动创建了
> db.blog.find() //看结果,就知道
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "number"
: 1, "title" : "My Blog Post" }
> db.blog.update({title:'My Blog Post'},{$inc:{number:1}}) //number存在,number+1最终结果为2
> db.blog.find()
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "number"
: 2, "title" : "My Blog Post" } www.zzzyk.com
>
> db.blog.update({title:'My Blog Post'},{$push:{push:1}}) //$push为数组类型字段添加一个值
> db.blog.find()
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "number"
: 2, "push" : [ 1 ], "title" : "My Blog Post" }
> db.blog.update({title:'My Blog Post'},{$pop:{push:1}}) //$pop删除数组中的一个值
> db.blog.find()
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "number"
: 2, "push" : [ ], "title" : "My Blog Post" }
>
> db.blog.ensureIndex({title:1}) //创建索引
> db.blog.find()
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "number"
: 2, "push" : [ ], "title" : "My Blog Post" }
> db.blog.dropIndex({title:1}) //删除索引
{ "nIndexesWas" : 2, "ok" : 1 } www.zzzyk.com
> db.blog.ensureIndex({title:1},{unique:true}) //创建unique索引 独立索引
> db.blog.find()
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "number"
: 2, "push" : [ ], "title" : "My Blog Post" }
>
> db.blog.ensureIndex({title:1,number:-1}) //创建联合索引
> db.blog.find()
{ "_id" : ObjectId("50264a762d31a25926fc6e86"), "comments" : [ ], "content" : "H
ere is my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "number"
: 2, "push" : [ ], "title" : "My Blog Post" }
> db.blog.dropIndex({title:1,number:-1})
{ "nIndexesWas" : 2, "ok" : 1 }
>
F:\Mongodb206\bin>mongodump.exe --db local --out backup //备份数据库local为backup文件夹中
connected to: 127.0.0.1
DATABASE: local to backup/local
local.blog to backup/local/blog.bson
1 objects www.zzzyk.com
local.system.indexes to backup/local/system.indexes.bson
1 objects
F:\Mongodb206\bin>mongorestore.exe -collection blog backup/local/blog //将备份数据还原
connected to: 127.0.0.1
DATABASE: local to backup/local
local.blog to backup/local/blog.bson
1 objects
local.system.indexes to backup/local/system.indexes.bson
1 objects