Javascript 用于保存用户联系人的MongoDB模式优化

Javascript 用于保存用户联系人的MongoDB模式优化,javascript,node.js,mongodb,mongoose-schema,Javascript,Node.js,Mongodb,Mongoose Schema,我想设计一个存储用户联系人的模式 以下是我现有的模式: 用户模式 _id : ObjectId("5c53653451154c6da4623a77"), name : “something”, email : “something”, password : “something”, "_id" : ObjectId("5c53653451154c6da4623a88"), user_id - ref mobile : “something”, company” : “something”, d

我想设计一个存储用户联系人的模式

以下是我现有的模式:

用户模式

_id : ObjectId("5c53653451154c6da4623a77"),
name : “something”,
email : “something”,
password : “something”,
"_id" : ObjectId("5c53653451154c6da4623a88"),
user_id - ref
mobile : “something”,
company” : “something”,
designation : “something”,
website : “something”,
social: {
    youtube: {
        type: String
    },
    twitter: {
        type: String
    },
    facebook: {
        type: String
    },
    linkedin: {
        type: String
    },
    instagram: {
        type: String
    }
}
配置文件架构

_id : ObjectId("5c53653451154c6da4623a77"),
name : “something”,
email : “something”,
password : “something”,
"_id" : ObjectId("5c53653451154c6da4623a88"),
user_id - ref
mobile : “something”,
company” : “something”,
designation : “something”,
website : “something”,
social: {
    youtube: {
        type: String
    },
    twitter: {
        type: String
    },
    facebook: {
        type: String
    },
    linkedin: {
        type: String
    },
    instagram: {
        type: String
    }
}
我可以想出两种联系方式,但都有一些缺点:

第一种方法

"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id - ref,
"contacts": [
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
   },
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
    },
    ...
]
"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id : ref,
contacts: [
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    ...
]
上述结构的问题是,当用户更新其配置文件时,联系人字段无法获得更新的值。但在这种方法中,很容易查询和检索特定用户的所有联系人并将响应发送回

第二种方法

"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id - ref,
"contacts": [
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
   },
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
    },
    ...
]
"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id : ref,
contacts: [
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    ...
]
在此结构中,当用户更新其配置文件时,联系人具有更新的用户值。但这里的问题是,在查询时,我必须从联系人模式中获取配置文件id,然后查询配置文件模式并将值作为响应返回给客户端

当有30K-50K个联系人时会发生什么情况?我是否需要查询数据库50K次?还是有更好的方法


在node.js上构建,使用mongoose。

有两种模式,一种用于用户,另一种用于订单。一个用户可以像您的联系人一样创建多个订单

使用者-


您的问题实际上是为什么mongodb不是这个场景的最佳选择。您的数据是关系型的,但您使用的是非关系型数据库


但对于您的情况,有一些步骤可以改善:

第一种方法

"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id - ref,
"contacts": [
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
   },
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
    },
    ...
]
"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id : ref,
contacts: [
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    ...
]
您可以选择第一种方法,并在更新用户数据时更新所有联系人。更容易查询,但难以保持数据同步

第二种方法

"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id - ref,
"contacts": [
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
   },
   {
    name : “something”,
    company : “something”,
    designation : “something”,
    website : “something”,
    social: { something },
    mobile : “something”
    },
    ...
]
"_id" : ObjectId("5c53653451154c6da4623a99"),
user_id : ref,
contacts: [
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    profile_id,
    ...
]

或者您可以选择第二种方法,但不只是保存ref_id,还可以保存联系人姓名(该字段主要用于查询)。通过这种方式,可以更轻松地保持数据同步,如果按名称进行搜索,您可以进行正常查找。

如果我理解正确,我认为您已经正确识别了每个选项的一些优点/缺点。现在你必须决定什么对你的具体情况有意义。选项1将很容易获取,但更新和保持与
Profile
s的同步将非常繁琐。选项2有更多的标准化数据,更适合更新,但需要更多的查询来检索。所以你必须问自己一些问题

  • 规范化数据对您有多重要
  • 您的
    个人资料
    联系人
    模式的大小如何比较?你会有更多的个人资料吗?显著减少?数量级
  • 更经常发生的事情是什么?有人更新他们的个人资料,或者有人查询联系人?还要多少?数量级
  • 为了更深入地了解你的最后两个答案,做一些估计,如果有必要,甚至是粗略的估计,然后做一些数学运算,看看什么更有意义
例如,如果每个用户有10000个联系人,那么选项1将为您提供一个更大的文档,可以在一个查询中收集所有联系人和个人资料。如果你的用户只更新他们的个人资料,比如说,平均每月更新一次,但是你需要一天查询几次联系人,那么这可能是更好的选择。然而,如果你有喜欢每天更新个人资料的用户,你可能需要每周查询一次联系人,那么选项2可能更有意义


归根结底,这是一个非常特定于您的场景的设计选择。祝你好运

基本上,您有一个需要关系数据库的场景。但您也可以在mongo中实现这一点

您需要使用猫鼬的填充。用你的第二种方法。您可以在其中存储配置文件ID

User.find({_id: '5c53653451154c6da4623a77'}).populate({
path:'profiles',
options: {
    limit: 10,
    skip: 0
}}).exec();

此查询将返回相关的配置文件。如果你有像50K这样的数据。必须将数据限制在一个请求中

注意:每个文档的Mongodb限制为16mb。不可能存储那么多数据。


所以,重新考虑一下你的数据库

一个用户是否有多个联系人?用户架构和配置文件架构应该相同。不,这就像一个用户正在联系人数组中保存另一个用户的联系人,依此类推。他可以想存多少就存多少。用户模式基本上是用来登录的。Rest用户必须创建自己的配置文件,并保存其他用户的联系人(即配置文件)。您应该为一个联系人使用一个文档,并按用户ID引用。(谁保存的)不,它与我的不一样,尽管我有一个问题可能会解决我的问题:如何获取所有下单用户的配置文件信息?就像有5万个用户下了订单,你必须显示他们的个人资料,用户模式中的所有信息?然后你必须在mongodbYou中使用聚合你将获取所有订单和groupby用户ID并获取下订单的用户。是的,但我的项目太重太复杂了。好的,没问题,请澄清一件事:所以在获取用户时,我必须查询数据库的次数与用户计数相同?