当前位置:Gxlcms > 数据库问题 > mongodb笔记1(基本操作,增删改)

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

时间:2021-07-01 10:21:17 帮助过:39人阅读

  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(基本操作,增删改)

标签:insert   数据库   

人气教程排行