Docs/数据库/mongodb/MongoDB数据表基本操作.md

3.5 KiB
Raw Blame History

MongoDB数据表基本操作

查看全部数据表

> use ChatRoom
switched to db ChatRoom
> show collections
Account
Chat
system.indexes
system.users

创建数据表

> db.createCollection("Account")
{"ok":1}
> db.createCollection("Test",{capped:true, size:10000}) { "ok" : 1 }
{"ok":1}

-- 说明

capped:true表示该集合的结构不能被修改

size:在建表之初就指定一定的空间大小接下来的插入操作会不断地按顺序APPEND数据在这个预分配好空间的文件中如果已经超出空间大小则回到文件头覆盖原来的数据继续插入。这种结构保证了插入和查询的高效性它不允许删除单个记录更新的也有限制不能超过原有记录的大小。这种表效率很高它适用于一些暂时保存数据的场合比如网站中登录用户的session信息又比如一些程序的监控日志都是属于过了一定的时间就可以被覆盖的数据。

修改数据表名

> db.Account.renameCollection("Account1")
{ "ok" : 1 }

数据表帮助主题help

> db.Account.help()
DBCollection help
        db.Account.find().help() - show DBCursor help
        db.Account.count()
        db.Account.dataSize()
        db.Account.distinct( key ) - eg. db.Account.distinct( 'x' )
        db.Account.drop() drop the collection
        db.Account.dropIndex(name)
        db.Account.dropIndexes()
        db.Account.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
        db.Account.reIndex()
        db.Account.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to retur.

                                                      e.g. db.Account.find( {x:77} , {name:1, x:1} )
        db.Account.find(...).count()
        db.Account.find(...).limit(n)
        db.Account.find(...).skip(n)
        db.Account.find(...).sort(...)
        db.Account.findOne([query])
        db.Account.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
        db.Account.getDB() get DB object associated with collection
        db.Account.getIndexes()
        db.Account.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
        db.Account.mapReduce( mapFunction , reduceFunction , <optional params> )
        db.Account.remove(query)
        db.Account.renameCollection( newName , <dropTarget> ) renames the collection.
        db.Account.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
        db.Account.save(obj)
        db.Account.stats()
        db.Account.storageSize() - includes free space allocated to this collection
        db.Account.totalIndexSize() - size in bytes of all the indexes
        db.Account.totalSize() - storage allocated for all data and indexes
        db.Account.update(query, object[, upsert_bool, multi_bool])
        db.Account.validate() - SLOW
        db.Account.getShardVersion() - only for use with sharding 

查看全部表记录

> db.Account.find()
{ "_id" : ObjectId("4df08553188e444d001a763a"), "AccountID" : 1, "UserName" : "libing", "Password" : "1", "Age" : 26, "Email" : "libing@126.com", "RegisterDate" : "2011-06-09 16:31:25" }
{ "_id" : ObjectId("4df08586188e444d001a763b"), "AccountID" : 2, "UserName" : "lb", "Password" : "1", "Age" : 25, "Email" : "libing@163.com", "RegisterDate" : "2011-06-09 16:36:95" }