Is there a convention to name collection in MongoDB?

后端 未结 4 1213
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 07:36

I would like to know if there is a convention for database collections such as:

PageVisit or page_visit.

Are there any advantages/d

4条回答
  •  一整个雨季
    2020-12-02 08:19

    In http://docs.mongodb.org/manual/reference/limits/, the manual states that the collection names should begin with an underscore ('_') or a letter character, and cannot:

    • contain the $.
    • be an empty string (e.g. "").
    • contain the null character.
    • begin with the system. prefix. (Reserved for internal use.)

    However, if you follow the rule and create a collection with '_' as the beginning letter, such as "_TWII", you will encounter trouble when you want to drop the collection. See the test below and the way to fix it. Collection '_TWII' was created under 'people' db.

    > show collections
    _TWII
    employees
    system.indexes
    > db._TWII.drop()
    2015-02-19T16:34:56.738-0800 TypeError: Cannot call method 'drop' of undefined
    
    > use admin
    switched to db admin
    
    > db.runCommand({renameCollection:"people._TWII",to:"people.TWII"})
    { "ok" : 1 }
    > use people
    switched to db people
    > show collections
    TWII
    employees
    system.indexes
    > db.TWII.drop()
    true
    > show collections
    employees
    system.indexes
    > 
    

    A short-cut to delete _TWII collection while under 'people' db:

    > db.createCollection('^TWII')
    { "ok" : 1 }
    > db.getCollection('^TWII').drop()
    true
    

提交回复
热议问题