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" }
> 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" }
>

> 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 }
> 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
        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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值