首页 > 数据库技术 > 详细

mongodb笔记1(基本操作,增删改)

时间:2015-07-19 06:40:32      阅读:246      评论:0      收藏:0      [点我收藏+]
  1. 查询现有数据库

>show dbs


2.创建数据库,需要创建集合这个库才创建

>use mydb


3.查看集合

>show collections
或者
>show tables


4.创建文档并插入数据

>db.userInfo.insert({_id:1,name:"xiaoming"})


5.批量插入文档,shell是不支持批量插入的,要想完成批量插入可以用mongodb的应用驱动或者shell的for循环

>for(var i =0;i<10;i++){
..db.userInfo.insert({name:i})
..}


6.查看文档内容

>db.userInfo.find()


7.save操作,save操作和insert操作区别在于遇到_id相同的情况下

save完成保存操作

insert则会报错

> db.userInfo.insert({_id:1,name:"xiaoming"})
WriteResult({ "nInserted" : 1 })
> db.userInfo.insert({_id:1,name:"xiaocang"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: foobar.userInfo.$_id_  dup key: { : 1.0 }"
}
})
> db.userInfo.save({_id:1,name:"xiaocang"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


8.删除列表中所有数据,集合和索引不会被删除

> db.userInfo.remove({})
WriteResult({ "nRemoved" : 1 })
> db.userInfo.find()
> show collections
system.indexes
userInfo


9.根据条件删除 

db.[documentName].remove({})

删除集合中的name等于xiaoming的记录

db.userInfo.remove({name:"xiaoming"})




本文出自 “每天进步一点点” 博客,请务必保留此出处http://08jw3.blog.51cto.com/998816/1675975

mongodb笔记1(基本操作,增删改)

原文:http://08jw3.blog.51cto.com/998816/1675975

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!