日期:2014-05-16  浏览次数:20331 次

MongoDB (二) shell操作

库:database

表:collection

行:document

字段:对应键的值

mongo的每一个document的key可以不一样

基于BSON的find函数

?

MongoDB的数据类型

null boolean(true | false)? int float string

object ID(new ObjectId())? date(new Date())

timestamp? array? inner_document({key: value, key: {key: value, ...}})

RegExp

?

1../mongo ip/db (默认localhost/test)

2.创建db??? use db_name? 如果没有创建任何集合,系统会删除这个db

3.添加用户和认证?? db.addUser("name", "passwd", [true | false(是否只读)])? db.auth("name", "passwd")

admin数据库下添加用户和认证可以管理所有的数据库

4.db.document.insert({key: value, ...})?? 自动创建_id

5.show dbs? 查看数据库

6.show collections

7.db.users.insert({name: "sosop", age: 26})

8.db.users.find()

9. db.users.update({name: "sosop"}, {$set:{age: 25}})

10.db.users.update({name: "sosop"}, {$inc:{age: 1}})

11.db.users.update({name: "kent"}, {$unset:{age:1}})

12.db.users.update({name: "kent"}, {$push:{arr:28}})

13.db.users.update({name: "kent"}, {$pop:{arr:2}})

14.db.users.update({name: "kent"}, {$pull:{arr: 1}})

15.db.users.update({name: "kent"}, {$rename:{'arr':'a'}})

16.db.users.findOne()

17.db.users.findOne({name: "..."}) 带参数查询

18.db.users.find({}, {age: 0})? 返回除了age外的所有字段

19.db.user.find({condition}, {name: 1}) 返回条件符合的name字段

20.db.user.find({age: {$gt | $lt | $gte | $lte | ne [, $gt] :20}})

21.db.user.find:? $all: [.....]?? $in: [.....]?? $nin: [.....]

$exists: true | false???? $or [{},{},....]?? $nor [{},{},....]

{arr: {$size: n}}? {arr: {$type: n}}? {name: /regexp/}

22.db.users.find().sort({age: 1})

23.db.user.distinct("")

24.db.users.find().skip(n)

25.db.users.find().limit(1)

26.db.users.find().skip(n).limit(m)

27.db.users.find({age: 26}, {name: 1}, 1, 2)???? params: condition, fields, limit, skip

28.db.users.find().count()

29.db.users.remove({condition})

30.db.users.ensureIndex({field: 1})

31.db.users.getIndexs

32.db.users.dropIndexs

33.db.users.dropIndex({condition})

34.db.users.reIndex({})

35.db.system.users.find()

36.db.removeUser(username)

37.db.system.users.remove({username: ''...})

38.db.users.stats

39.db.users.drop()

40.db.cloneDatabase(fromHost)

41.db.copyDatabase(fromDB, toDB, fromHost)

42.db.dropDatabase()

43.db.repairDatabase()

44.db.shutdownServer()

?

?

最重要的? db.help()?? db.collection.help()