Mongodb 入门

 

SQL术语/概念                MongoDB术语/概念                          解释/说明
database database 数据库
table collection 数据库表/集合
row document 数据记录行/文档
column field 数据字段/域
index index 索引
table joins   表连接,MongoDB不支持
primary key primary key 主键,MongoDB自动将_id字段设置为主键

 

1.登录

 ./bin/mongo 127.0.0.1:27017/admin

默认使用test用户登录

 

2.创建一个数据库

use[databaseName]

但是如果什么都不做就离开,那么这个空的数据库就会被删除掉

 

3.查看所有的数据库

show dbs (默认有一个local数据库,另一个是我自己新建的)

> show dbs

foobar  0.000GB

local   0.000GB

 

4. 给制定的数据库加入数据

> db.persons.insert({name:"xizheng"})

db代表当前的数据库,persons是要新建的表名,后面是内容,bson是和json类似的,以键值对的形式存在。

 

5.查询数据库中的所有文档

> show collections

persons

 

6.查询特定的文档

> db.persons.find()

{ "_id" : ObjectId("567113d73547c89033c6a07b"), "name" : "xizheng" }

{ "_id" : ObjectId("567114883547c89033c6a07c"), "name" : "dashen" }

_id这个字段是mongo自动生成的

查询第一条数据

> db.persons.findOne()

{ "_id" : ObjectId("567113d73547c89033c6a07b"), "name" : "xizheng" }

 

7.update文档数据

> db.persons.update({name:"xizheng"},{$set:{name:"xizheng1"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.persons.findOne()

{ "_id" : ObjectId("567113d73547c89033c6a07b"), "name" : "xizheng1" }

update的第一个参数是查询条件,第二个是要更新的值,mongo默认修改第一条找到的记录

或者:

var p  = db.persons.findOne()

db.persons.udpate(p,{name:"xxxx"})

如果不用set修改器,那么会出现以下情况:

> var p =db.persons.findOne();db.persons.update(p,{age:1})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.persons.findOne();

{ "_id" : ObjectId("567113d73547c89033c6a07b"), "age" : 1 }

以前的数据就会丢失掉,这种被称为强硬的文档更新操作,会用新的文档代替旧的文档

强硬更新的时候如果有主键冲突,会停止更新

 

 

8.删除数据

db.persons.remove({...})

> db.persons.remove({name:"dashen"})

WriteResult({ "nRemoved" : 1 })

> db.persons.find()

{ "_id" : ObjectId("567113d73547c89033c6a07b"), "name" : "xizheng" }

 

 9.删除数据库中的集合

> db.persons.drop()

10. 删除数据库

> db.dropDatabase()

11. shell的help

> db.help()

DB methods:

db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]

db.auth(username, password)

db.cloneDatabase(fromhost)

..........

> db.persons.help()

DBCollection help

db.persons.find().help() - show DBCursor help

db.persons.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j

db.persons.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS

遇到不会写的查询或操作,可以使用help来查询,找到需要的命令。

 

12.mongodb的API

http://api.mongodb.org/

13.命名规则

1)不能是空字符串

2)不能含有空格,逗号,美元符号($),斜杠(/),反邪杠(\),

3)应全部小写

4)最多64个字节

5)不能和现有保留名同名,如admin,local及config

13. mongodb的shell内置javascript引擎可以直接执行js代码

14. shell可以用eval,不过已经deprecated了

> db.eval("return 'mongodb'")

WARNING: db.eval is deprecated

mongodb

15. BSON类型(后面补上)

16. 插入

db.[documentNamen].insert({})

17. 批量插入文档

shell用for循环

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

WriteResult({ "nInserted" : 1 })

> db.persons.find()

{ "_id" : ObjectId("568b61a8dd9f4b521be7542e"), "name" : 0 }

{ "_id" : ObjectId("568b61a8dd9f4b521be7542f"), "name" : 1 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75430"), "name" : 2 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75431"), "name" : 3 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75432"), "name" : 4 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75433"), "name" : 5 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75434"), "name" : 6 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75435"), "name" : 7 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75436"), "name" : 8 }

{ "_id" : ObjectId("568b61a8dd9f4b521be75437"), "name" : 9 }

18. save操作

save和insert的区别,相同ID 的情况下,save不会抛错,insert会抛错.

 > db.persons.insert({_id:"001",name:"xxx"})

WriteResult({ "nInserted" : 1 })

> db.persons.find()

{ "_id" : "001", "name" : "xxx" }

> db.persons.insert({_id:"001",name:"xxx"})

WriteResult({

"nInserted" : 0,

"writeError" : {

"code" : 11000,

"errmsg" : "E11000 duplicate key error collection: foobar.persons index: _id_ dup key: { : \"001\" }"

}

})

> db.persons.save({_id:"001",name:"xxx"})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

> db.persons.find()

{ "_id" : "001", "name" : "xxx" }

>  

19. 删除列表中的所有数据

db.[documentName].remove() 集合的本身和索引不会被删除

20. 根据条件删除

db.[documentName].remove({name:"xxxx"})

如果你想清除一个数据量十分庞大的集合,直接删除该集合并且重建比remove效率要高

21. insertOrUpdate操作

查询到就更新,查询不到就插入

db.[documentName].update({查询器},{修改器},true)

22.批量更新操作

必须配合修改器

> db.persons.find()

{ "_id" : "001", "name" : "xxx" }

{ "_id" : "002", "name" : "xxx" }

> db.persons.update({name:"xxx"},{$set:{name:"333"}},false,true)

WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })

> db.persons.find()

{ "_id" : "001", "name" : "333" }

{ "_id" : "002", "name" : "333" }

 

 

 

 

 

猜你喜欢

转载自zhengxiangbin2008.iteye.com/blog/2264525