【问题标题】:Mongoengine collection naming with UpperCamelCase?Mongoengine集合命名与UpperCamelCase?
【发布时间】:2021-02-02 19:02:11
【问题描述】:

我一直在使用 mongoengine 模型向 Flask 应用程序添加一些集合,并且代码运行良好。 我正在使用 MongoCompass 检查数据,只是注意到其中一个集合名为 notify_destination,这不是我使用的名称或其小写版本。

我的模型类是 NotifyDestination 并且没有元标记 - 那么为什么集合名称中间有下划线?

class NotifyDestination(me.Document):
    owner_id = me.ObjectIdField()
    username = me.StringField()

Mongoengine 文档 (2.3.4) 只是说 集合名默认为类名,转为小写。

由于我使用 UpperCamelCase,MongoEngine 的下划线插入正常吗?

如果此行为未在其他地方正式记录,我仍有时间在模型中使用 meta = {} 标记指定和强制命名。

【问题讨论】:

    标签: mongoengine


    【解决方案1】:

    (这里是mongoengine 贡献者)是的,这是默认行为,文档不精确,但它基本上是从UpperCamelCase 转换为snake_case。

    这意味着

    class User(Document):
        # will have "user" as default collection name
    
    class MyCompanyUser(Document):
        # will have "my_company_user" as default collection name
    
    class USACompany(Document):
        # will have "u_s_a_company" as default collection name
    

    请注意,该文档已修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多