MongoDB(猫鼬)数据结构问题 [英] MongoDB (Mongoose) data structure question

查看:77
本文介绍了MongoDB(猫鼬)数据结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在Mongo中代表这种情况的最佳方式感到好奇.我有自己的想法,但对总体共识/最佳实践实际上是什么感到好奇.

I'm curious about the best way to represent this kind of situation in Mongo. I have my own idea, but I'm curious on what the general consensus/best practice actually would be.

想象一下我有两个收藏夹:-

Imagine I have two collections:-

Employees
 --> _id
 --> FirstName
 --> Surname
 --> Email

Comments
  --> _id
  --> PersonReference
  --> CommentDate
  --> Comment

现在想象一下,员工可以来去去了,并且员工"集合始终是最新的.但是,如果员工曾经发表过评论,则必须提供评论的完整信息,包括发表评论的人.

Now imagine that Employees can come and go and the 'Employees' collection is always up-to-date. However, in the event that an employee has ever made a comment, the full information on the comment including who made it must be available.

我建议解决此问题的方法是组织这样的结构:-

The way I would propose to tackle this problem, is to organise the structure like this instead:-

Employees
 --> _id: _id
 --> FirstName: string
 --> Surname: string
 --> Email: string

Comments
  --> _id: _id
  --> CommentDate: date
  --> Comment: string
  [-] --> PersonReference
  [+] --> Employee: object { _id: id, FirstName: string, Surname: string, Email:string }

因此,从本质上讲,我将拥有现役员工"列表,并且在发表评论时,我会将员工信息复制到评论"收集文档中(而不是使用参考).

So essentially, I would have a list of 'Active Employees' and at a time where a comment is made, I would duplicate the employee information into the Comments collection document (rather than use a reference).

从较高的角度来看,这被认为是最佳实践吗?

From a high level perspective, is this considered best practise?

非常感谢

推荐答案

在注释集合中复制员工信息确实不是一个好主意. 当需要更改员工信息时,还需要在注释中对其进行更新.

Duplicating the employee info in the comments collection is really a bad idea. When an employee info needs to be changed, it will also needs to be updated in the comments.

您有几种选择:

1-)将注释嵌入Employee模式中:

1-) Embedding the comments inside the Employee schema:

在这种方法中,我们没有单独的注释集合.

In this method we have no separate Comments collection.

如果您不需要独立查询注释,则此方法很有意义. 通过这种方式,我们可以在一次数据库访问中访问用户及其注释,而无需任何连接(填充或查找).

If you have no need to independently query comments, this method makes sense. This way we can access a user and his/her comments in one db access and without needing any join (populate or lookup).

其架构可以是这样的:

const mongoose = require("mongoose");

const employeeSchema = new mongoose.Schema({
  firstName: String,
  username: String,
  email: String,
  comments: [
    new mongoose.Schema({
      commentDate: Date,
      comment: String
    })
  ]
});

module.exports = mongoose.model("Employee", employeeSchema);

2-)父母参考:

在这种方法中,我们将注释的引用保留在Employee模式中. 如果您不需要通过评论访问员工,则可以选择此选项.

In this method we keep the reference of the comments in the Employee schema. If you don't need to access to employee from a comment, this can an option.

员工架构:

const mongoose = require("mongoose");

const employeeSchema = new mongoose.Schema({
  firstName: String,
  username: String,
  email: String,
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Comment"
    }
  ]
});

module.exports = mongoose.model("Employee", employeeSchema);

评论模式:

const mongoose = require("mongoose");

const commentSchema = new mongoose.Schema({
  commentDate: Date,
  comment: String
});

module.exports = mongoose.model("Comment", commentSchema);

3-)引用儿童

在这种方法中,我们在注释中保留员工的引用. 因此,如果您需要访问员工的评论,我们需要使用填充虚拟猫鼬的功能.在员工模式中,我们没有对注释的引用.

In this method we keep reference of the employee in the comments. So if you need to access the comments from an employee we need to use Populate Virtual feature of mongoose. Becase in employee schema we don't have a reference to the comments.

员工架构:

const mongoose = require("mongoose");

const employeeSchema = new mongoose.Schema(
  {
    firstName: String,
    username: String,
    email: String
  },
  {
    toJSON: { virtuals: true } // required to use populate virtual
  }
);

// Populate virtual
employeeSchema.virtual("comments", {
  ref: "Comment",
  foreignField: "employee",
  localField: "_id"
});

module.exports = mongoose.model("Employee", employeeSchema);

评论模式:

const mongoose = require("mongoose");

const commentSchema = new mongoose.Schema({
  commentDate: Date,
  comment: String,
  employee: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Employee"
  }
});

module.exports = mongoose.model("Comment", commentSchema);

4-)父级和子级都引用:

4-) Both parent and child referencing:

使用这种方法,可以从员工中选择注释,并从注释中选择员工.但是这里我们有某种形式的数据重复,并且当删除注释时,两个集合中都需要完成它.

With this method, it is possible to select comments from employee, and employee from comments. But here we have some kind of data duplication, and also when a comment is deleted, it needs to be done in both of the collections.

const mongoose = require("mongoose");

const employeeSchema = new mongoose.Schema({
  firstName: String,
  username: String,
  email: String,
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Comment"
    }
  ]
});

module.exports = mongoose.model("Employee", employeeSchema);

评论模式:

const mongoose = require("mongoose");

const commentSchema = new mongoose.Schema({
  commentDate: Date,
  comment: String,
  employee: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Employee"
  }
});

module.exports = mongoose.model("Comment", commentSchema);

这篇关于MongoDB(猫鼬)数据结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆