From 9805c4a1029f21591fb3ada9211bb31dd52e1af3 Mon Sep 17 00:00:00 2001 From: iProbe Date: Thu, 2 Feb 2023 16:13:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93/mongodb/MongoDB=E6=95=B0=E6=8D=AE=E8=A1=A8=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E6=93=8D=E4=BD=9C.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 数据库/mongodb/MongoDB数据表基本操作.html | 210 ---------------------- 数据库/mongodb/MongoDB数据表基本操作.md | 78 ++++++++ 2 files changed, 78 insertions(+), 210 deletions(-) delete mode 100644 数据库/mongodb/MongoDB数据表基本操作.html create mode 100644 数据库/mongodb/MongoDB数据表基本操作.md diff --git a/数据库/mongodb/MongoDB数据表基本操作.html b/数据库/mongodb/MongoDB数据表基本操作.html deleted file mode 100644 index d1d9318..0000000 --- a/数据库/mongodb/MongoDB数据表基本操作.html +++ /dev/null @@ -1,210 +0,0 @@ - - - - - -MongoDB数据表基本操作 - - - - - - - -
-

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" }

 

-
- - - - - \ No newline at end of file diff --git a/数据库/mongodb/MongoDB数据表基本操作.md b/数据库/mongodb/MongoDB数据表基本操作.md new file mode 100644 index 0000000..9e07bae --- /dev/null +++ b/数据库/mongodb/MongoDB数据表基本操作.md @@ -0,0 +1,78 @@ +# MongoDB数据表基本操作 +## 查看全部数据表 +```bash +> use ChatRoom +switched to db ChatRoom +> show collections +Account +Chat +system.indexes +system.users +``` +## 创建数据表 +```bash +> db.createCollection("Account") +{"ok":1} +> db.createCollection("Test",{capped:true, size:10000}) { "ok" : 1 } +{"ok":1} +``` +-- 说明 + +capped:true,表示该集合的结构不能被修改; + +size:在建表之初就指定一定的空间大小,接下来的插入操作会不断地按顺序APPEND数据在这个预分配好空间的文件中,如果已经超出空间大小,则回到文件头覆盖原来的数据继续插入。这种结构保证了插入和查询的高效性,它不允许删除单个记录,更新的也有限制:不能超过原有记录的大小。这种表效率很高,它适用于一些暂时保存数据的场合,比如网站中登录用户的session信息,又比如一些程序的监控日志,都是属于过了一定的时间就可以被覆盖的数据。 + + + +## 修改数据表名 +```bash +> db.Account.renameCollection("Account1") +{ "ok" : 1 } + ``` + +## 数据表帮助主题help +```bash +> 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 , ) + db.Account.remove(query) + db.Account.renameCollection( newName , ) renames the collection. + db.Account.runCommand( name , ) 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 +``` + +## 查看全部表记录 +```bash +> 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" } +``` + \ No newline at end of file