mongodb增删改查基本操作

mongodb中的数据结构和rdbms database中的对应关系如下:
SQL术语/概念 MongoDB术语/概念 解释/说明
database database 数据库
table collection 数据库表/集合
row document 数据记录行/文档
column field 数据字段/域
index index 索引
table joins   表连接,MongoDB不支持
primary key primary key 主键,MongoDB自动将_id字段设置为主键
对于数据库,无需创建,直接使用use database 命令切换到指定的database,如果database不存在,那么在创建表的时候会自动创建database.如果存在,则切换到此数据库.
对于数据库集合,也不需要创建,因为对于mongodb来说并没有所谓的表结构,直接操作即可,如果不存在那么在insert的时候会自动创建此集合.
下面是基本的操作方法

1.创建集合

使用insert函数将文档添加到集合中,例如当前有一个post变量,是一个js对象表示我们的文档.使用 db.collectionname.insert()来插入数据.
当前是没有blog这张表的
    
    
> show collections
persans
插入数据:
    
    
> post={'title':'my blog','content':'this is my first blog','date':new Date()}
{
"title" : "my blog",
"content" : "this is my first blog",
"date" : ISODate("2016-05-15T10:16:23.961Z")
}
> db.blog.insert(post)
WriteResult({ "nInserted" : 1 })
>
> show collections;
blog
persans
可以看到是直接创建了这个集合,然后插入了数据.

2.查询集合

查询使用find方法, db.collectionname.find(),例如:
    
    
> db.blog.find()
{ "_id" : ObjectId("57384c85132e1e47e535a172"), "title" : "my blog", "content" : "this is my first blog", "date" : ISODate("2016-05-15T10:16:23.961Z") }
{ "_id" : ObjectId("57384e65132e1e47e535a173"), "0" : "G", "1" : "H", "2" : "I", "3" : "J", "4" : "K", "5" : "L", "6" : "M", "7" : "N", "8" : "O", "9" : "P", "10" : "K", "11" : "L" }
{ "_id" : ObjectId("57384e71132e1e47e535a174"), "3" : "Total", "4" : "Used", "5" : "Free" }
{ "_id" : ObjectId("57384e82132e1e47e535a175"), "0" : "W", "1" : "X", "2" : "Y", "3" : "Z", "4" : "AG", "5" : "AH", "6" : "AI", "7" : "AJ", "8" : "AK", "9" : "AL", "10" : "AM", "11" : "AN" }
如果只想查看一个文档,使用findOne
    
    
> db.blog.findOne()
{
"_id" : ObjectId("57384c85132e1e47e535a172"),
"title" : "my blog",
"content" : "this is my first blog",
"date" : ISODate("2016-05-15T10:16:23.961Z")
}

3.更新集合

更新使用update方法, db.collectionname.update({},{})
update方法至少接受两个参数,第一个是限定条件,用来匹配更新的文档,第二个是新的文档. 
首先修改变量post,增加一个comments键,然后使用update替换,具体如下:
    
    
> post.comments=[]
[ ]
这个的意思是给post加了一个键为comments,值为空数组
此时的post为:
    
    
> post
{
"title" : "my blog",
"content" : "this is my first blog",
"date" : ISODate("2016-05-15T10:16:23.961Z"),
"comments" : [ ]
}
此时update:
    
    
> db.blog.update({title:"my blog"},post)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.find()
{ "_id" : ObjectId("57384c85132e1e47e535a172"), "title" : "my blog", "content" : "this is my first blog", "date" : ISODate("2016-05-15T10:16:23.961Z"), "comments" : [ ] }
{ "_id" : ObjectId("57384e65132e1e47e535a173"), "0" : "G", "1" : "H", "2" : "I", "3" : "J", "4" : "K", "5" : "L", "6" : "M", "7" : "N", "8" : "O", "9" : "P", "10" : "K", "11" : "L" }
{ "_id" : ObjectId("57384e71132e1e47e535a174"), "3" : "Total", "4" : "Used", "5" : "Free" }
{ "_id" : ObjectId("57384e82132e1e47e535a175"), "0" : "W", "1" : "X", "2" : "Y", "3" : "Z", "4" : "AG", "5" : "AH", "6" : "AI", "7" : "AJ", "8" : "AK", "9" : "AL", "10" : "AM", "11" : "AN" }
还可以不用post直接进行update:
    
    
> db.blog.update({"title":"my blog"},{"title":"hello"})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.blog.update({"title":"hello"},{"title":"hello2"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.find()
{ "_id" : ObjectId("57384c85132e1e47e535a172"), "title" : "hello2" }
{ "_id" : ObjectId("57384e65132e1e47e535a173"), "0" : "G", "1" : "H", "2" : "I", "3" : "J", "4" : "K", "5" : "L", "6" : "M", "7" : "N", "8" : "O", "9" : "P", "10" : "K", "11" : "L" }
{ "_id" : ObjectId("57384e71132e1e47e535a174"), "3" : "Total", "4" : "Used", "5" : "Free" }
{ "_id" : ObjectId("57384e82132e1e47e535a175"), "0" : "W", "1" : "X", "2" : "Y", "3" : "Z", "4" : "AG", "5" : "AH", "6" : "AI", "7" : "AJ", "8" : "AK", "9" : "AL", "10" : "AM", "11" : "AN" }

4.删除文档

使用remove方法可以将文档从数据库内永久删除, db.collectionname.remove({})
如果没有添加任何参数则会将所有文档删除
    
    
> db.blog.find()
{ "_id" : ObjectId("57384c85132e1e47e535a172"), "title" : "hello2" }
{ "_id" : ObjectId("57384e65132e1e47e535a173"), "0" : "G", "1" : "H", "2" : "I", "3" : "J", "4" : "K", "5" : "L", "6" : "M", "7" : "N", "8" : "O", "9" : "P", "10" : "K", "11" : "L" }
{ "_id" : ObjectId("57384e71132e1e47e535a174"), "3" : "Total", "4" : "Used", "5" : "Free" }
{ "_id" : ObjectId("57384e82132e1e47e535a175"), "0" : "W", "1" : "X", "2" : "Y", "3" : "Z", "4" : "AG", "5" : "AH", "6" : "AI", "7" : "AJ", "8" : "AK", "9" : "AL", "10" : "AM", "11" : "AN" }
> db.blog.remove({"_id" : ObjectId("57384c85132e1e47e535a172")})
WriteResult({ "nRemoved" : 1 })
> db.blog.find()
{ "_id" : ObjectId("57384e65132e1e47e535a173"), "0" : "G", "1" : "H", "2" : "I", "3" : "J", "4" : "K", "5" : "L", "6" : "M", "7" : "N", "8" : "O", "9" : "P", "10" : "K", "11" : "L" }
{ "_id" : ObjectId("57384e71132e1e47e535a174"), "3" : "Total", "4" : "Used", "5" : "Free" }
{ "_id" : ObjectId("57384e82132e1e47e535a175"), "0" : "W", "1" : "X", "2" : "Y", "3" : "Z", "4" : "AG", "5" : "AH", "6" : "AI", "7" : "AJ", "8" : "AK", "9" : "AL", "10" : "AM", "11" : "AN" }
     
     
> db.blog.remove({})
WriteResult({ "nRemoved" : 3 })
> db.blog.find()

mongodb中必须有一个_id键,这个键可以是任何类型,默认是一个ObjectId类型,每一个集合里每个文档都有唯一的_id,确保每个集合里面的每个的文档都能被唯一标识.
如果你不手工指定,那么mongodb会自动为你生成这个_id.
ObjectId使用12字节的存储,有24个16进制数字组成.例如" 57384e65132e1e47e535a173"
前4个字节表示时间戳,隐藏表示文档的创建时间: 57384e65->1463307877
    
    
[root@mongodb1 ~]# date -d '1970-01-01 UTC 1463307877sec'
Sun May 15 18:24:37 CST 2016
接下来3个字节表示主机名,一般是主机名的散列,确保不同的主机生成的不同ObjectId: 132e1e->1256990
接下来的2个字节表示进程的PID: 47e5->18405
前9个字节保证了同一秒钟不同机器机器不同进程产生的ObjectId是不同的: 35a173->3514739
最后3个字节是一个自动增加的计数器,确保相同的进程同一秒钟产生的objectId也是不一样的,一秒钟最多允许每个进程拥有256^3个不同的ObjectId.
注意,默认生成的ObjectId是客户端产生的,并非是由服务端产生的.

5.删除集合

使用方法 db.collectionname.drop()
    
    
> show collections
blog
persans
> db.blog.drop()
true
> show collections
persans

6.删除数据库

使用方法 db.dropDatabase()
    
    
> db.dropDatabase()
{ "dropped" : "suq", "ok" : 1 }
> show dbs
local 0.000GB







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值