Mongo Collections

MongoDB中的collections就是一系列 BSON documents的集合,至關於關係數據庫中的表。shell

collection將會在第一次往裏面插入documents時建立數據庫

  1. > show dbs;
    app

  2. admin    (empty)
    spa

  3. foo    0.0625GB
    it

  4. fucker    0.0625GB
    io

  5. local    (empty)
    test

  6. test    0.0625GB
    方法

  7. > use fucker;
    命名

  8. switched to db fucker
    數據

  9. > show collections;

  10. fucker

  11. system.indexes

  12. users

  13. > db.test.insert({"id" : 1 });

  14. > show collections;

  15. fucker

  16. system.indexes

  17. test

  18. users

collection應該以字母或下劃線開頭,固然數字也是被容許包含在collection名字內的($爲保留字符,不可用於命名)。

collection名最大長度爲128個字符,建議不要超80/90個字符


能夠使用以下命令來建立collection(通常用於建立Capped collections)

  1. > show collections;

  2. fucker

  3. system.indexes

  4. test

  5. users

  6. > //mongo shell

  7. > db.createCollection("mycoll",{capped:true, size:100000}) //size is in bytes

  8. { "ok" : 1 }

  9. > show collections;

  10. fucker

  11. mycoll

  12. system.indexes

  13. test

  14. users

或者使用以下方法
  1. > show collections;

  2. fucker

  3. mycoll

  4. system.indexes

  5. test

  6. users

  7. > db.runCommand( {create:"mycoll_1", capped:true, size:100000} )

  8. { "ok" : 1 }

  9. > show collections;

  10. fucker

  11. mycoll

  12. mycoll_1

  13. system.indexes

  14. test

  15. users

collection重命名
方法1:
  1. > show collections;

  2. fucker

  3. mycoll

  4. mycoll_1

  5. system.indexes

  6. test

  7. users

  8. > db.mycoll.renameCollection("Yourcoll");

  9. { "ok" : 1 }

  10. > show collections;

  11. Yourcoll

  12. fucker

  13. mycoll_1

  14. system.indexes

  15. test

  16. users

方法2:
  1. > show collections;

  2. Yourcoll

  3. fucker

  4. mycoll_1

  5. system.indexes

  6. test

  7. users

  8. > use admin;

  9. switched to db admin

  10. > db.runCommand( { renameCollection: "fucker.Yourcoll", to: "fucker.Hiscoll"} );

  11. { "ok" : 1 }

  12. > use fucker;

  13. switched to db fucker

  14. > show collections;

  15. Hiscoll

  16. fucker

  17. mycoll_1

  18. system.indexes

  19. test

  20. users

相關文章
相關標籤/搜索