DEV Community

Cover image for 📚 技巧和诀窍: MongoDB 属性模式 💻
Danny Chan for MongoDB Builders

Posted on

2 2 2 2 2

📚 技巧和诀窍: MongoDB 属性模式 💻

情况:

  • 单一视图应用程序 🔍
  • 融合任何类型的数据 🌐
  • 动态模式以便迭代 🔀
  • 表达性查询语言、索引和聚合以查找和过滤数据 🔍


挑战:

  • 多样的数据类型:从不同系统中协调模式很困难 🤯
  • rigid 模式:无法更改模式 📛


缺点:

  • 只能有细粒度的数据模式 📋
  • 无法添加临时查询 🚫
  • 无法添加辅助索引 🚫



解决方案: MongoDB 文档模型 💥

  • 使用任何类型的数据(数字、字符串、二进制数据、数组) 🔢🔤🔣🔢
  • 动态模式:不需要所有字段都一样 🔀
  • 查询示例:索引以查找和过滤数据 🔍
  • 索引: pet
{
    "name": "Peter"
}
{
    "name": "Mary",
    "pet": "cat"
}
Enter fullscreen mode Exit fullscreen mode


结果:

  • 只能查询 Mary,因为她在索引中有 pet 😕


属性模式: 🔑

  • 子集:包含许多相似字段的大型文档 📜
  • 将子集数据移动到键值子文档中 📂
  • 在少量文档的子集中对字段进行排序 🗂️


优势:

  • 避免创建大量索引并减少性能 💪


属性模式用例 1:保险公司 🏦

  • 将数据从多个来源聚合到一个中心位置 🌐
  • 获得客户的 360°视图 🔍
  • 以较低的成本提供更好的客户服务 💰
  • 快速创新 🚀



之前:

{
    release_Italy: ISODate("1977-10-20T01:00:00+01:00"),
    release_France: ISODate("1977-10-19T01:00:00+01:00"),
    release_UK: ISODate("1977-12-27T01:00:00+01:00"),
}
Enter fullscreen mode Exit fullscreen mode

索引:

{release_Italy: 1}
{release_France: 1}
{release_UK: 1}
Enter fullscreen mode Exit fullscreen mode



之后:

{
    releases: [
        {
        location: "USA",
        date: ISODate("1977-05-20T01:00:00+01:00")
        },
        {
        location: "France",
        date: ISODate("1977-10-19T01:00:00+01:00")
        }
    ],
}
Enter fullscreen mode Exit fullscreen mode

索引:

{ "releases.location": 1, "releases.date": 1}
Enter fullscreen mode Exit fullscreen mode



非确定性命名: 🔖

  • 轻松添加额外的限定符 🔍
  • 明确说明原始字段和值的关系 📝
{
    "specs": [
        { k: "volume", v: "500", u: "ml" },
        { k: "volume", v: "12", u: "ounces" }
    ]
}
Enter fullscreen mode Exit fullscreen mode

索引:

{"specs.k": 1, "specs.v": 1, "specs.u": 1}
Enter fullscreen mode Exit fullscreen mode



属性模式用例 2:服装 👕

  • 以 small、medium、large 表示尺寸 📏
  • 部分服装系列以数字表示(11、12、13) 🔢
  • 特征很少在资产中共同存在 🎨

产品目录中有相似的属性,如名称、供应商、制造商、原产地 🏭

  • 为数据提供良好的结构 🗂️
{
    "specs": [
        { k: "size", v: "11", u: "CHINA" },
        { k: "size", v: "large", u: "USA" }
    ]
}
Enter fullscreen mode Exit fullscreen mode

索引:

{"specs.k": 1, "specs.v": 1, "specs.u": 1}
Enter fullscreen mode Exit fullscreen mode



Reference:

https://www.mongodb.com/developer/products/mongodb/attribute-pattern/
Building with Patterns: The Attribute Pattern

https://www.mongodb.com/solutions/use-cases/single-view
Single View applications

https://www.mongodb.com/blog/post/building-with-patterns-a-summary
Building with Patterns: A Summary

https://www.mongodb.com/blog/post/building-with-patterns-the-approximation-pattern
Building with Patterns: The Approximation Pattern


Editor

Image description

Danny Chan, specialty of FSI and Serverless

Image description

Kenny Chan, specialty of FSI and Machine Learning

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay