collection将会在第一次往里面插入documents时创建
点击(此处)折叠或打开
- > show dbs;
- admin (empty)
- foo 0.0625GB
- fucker 0.0625GB
- local (empty)
- test 0.0625GB
- > use fucker;
- switched to db fucker
- > show collections;
- fucker
- system.indexes
- users
- > db.test.insert({"id" : 1 });
- > show collections;
- fucker
- system.indexes
- test
- users
collection名最大长度为128个字符,建议不要超80/90个字符
可以使用如下命令来创建collection(一般用于创建Capped collections)
点击(此处)折叠或打开
- > show collections;
- fucker
- system.indexes
- test
- users
- > //mongo shell
- > db.createCollection("mycoll",{capped:true, size:100000}) //size is in bytes
- { "ok" : 1 }
- > show collections;
- fucker
- mycoll
- system.indexes
- test
- users
点击(此处)折叠或打开
- > show collections;
- fucker
- mycoll
- system.indexes
- test
- users
- > db.runCommand( {create:"mycoll_1", capped:true, size:100000} )
- { "ok" : 1 }
- > show collections;
- fucker
- mycoll
- mycoll_1
- system.indexes
- test
- users
collection重命名
方法1:
点击(此处)折叠或打开
- > show collections;
- fucker
- mycoll
- mycoll_1
- system.indexes
- test
- users
- > db.mycoll.renameCollection("Yourcoll");
- { "ok" : 1 }
- > show collections;
- Yourcoll
- fucker
- mycoll_1
- system.indexes
- test
- users
点击(此处)折叠或打开
- > show collections;
- Yourcoll
- fucker
- mycoll_1
- system.indexes
- test
- users
- > use admin;
- switched to db admin
- > db.runCommand( { renameCollection: "fucker.Yourcoll", to: "fucker.Hiscoll"} );
- { "ok" : 1 }
- > use fucker;
- switched to db fucker
- > show collections;
- Hiscoll
- fucker
- mycoll_1
- system.indexes
- test
- users