MongoDB relationships: embed or reference?

后端 未结 11 2378
夕颜
夕颜 2020-11-21 05:34

I\'m new to MongoDB--coming from a relational database background. I want to design a question structure with some comments, but I don\'t know which relationship to use for

相关标签:
11条回答
  • 2020-11-21 05:43

    If I want to edit a specified comment, how to get its content and its question?

    You can query by sub-document: db.question.find({'comments.content' : 'xxx'}).

    This will return the whole Question document. To edit the specified comment, you then have to find the comment on the client, make the edit and save that back to the DB.

    In general, if your document contains an array of objects, you'll find that those sub-objects will need to be modified client side.

    0 讨论(0)
  • 2020-11-21 05:43

    Actually, I'm quite curious why nobody spoke about the UML specifications. A rule of thumb is that if you have an aggregation, then you should use references. But if it is a composition, then the coupling is stronger, and you should use embedded documents.

    And you will quickly understand why it is logical. If an object can exist independently of the parent, then you will want to access it even if the parent doesn't exist. As you just can't embed it in a non-existing parent, you have to make it live in it's own data structure. And if a parent exist, just link them together by adding a ref of the object in the parent.

    Don't really know what is the difference between the two relationships ? Here is a link explaining them: Aggregation vs Composition in UML

    0 讨论(0)
  • 2020-11-21 05:45

    Well, I'm a bit late but still would like to share my way of schema creation.

    I have schemas for everything that can be described by a word, like you would do it in the classical OOP.

    E.G.

    • Comment
    • Account
    • User
    • Blogpost
    • ...

    Every schema can be saved as a Document or Subdocument, so I declare this for each schema.

    Document:

    • Can be used as a reference. (E.g. the user made a comment -> comment has a "made by" reference to user)
    • Is a "Root" in you application. (E.g. the blogpost -> there is a page about the blogpost)

    Subdocument:

    • Can only be used once / is never a reference. (E.g. Comment is saved in the blogpost)
    • Is never a "Root" in you application. (The comment just shows up in the blogpost page but the page is still about the blogpost)
    0 讨论(0)
  • 2020-11-21 05:52

    I came across this small presentation while researching this question on my own. I was surprised at how well it was laid out, both the info and the presentation of it.

    http://openmymind.net/Multiple-Collections-Versus-Embedded-Documents

    It summarized:

    As a general rule, if you have a lot of [child documents] or if they are large, a separate collection might be best.

    Smaller and/or fewer documents tend to be a natural fit for embedding.

    0 讨论(0)
  • 2020-11-21 05:53

    Yes, we can use the reference in the document.To populate the another document just like sql i joins.In mongo db they dont have joins to mapping one to many relationship document.Instead that we can use populate to fulfill our scenario..

    var mongoose = require('mongoose')
      , Schema = mongoose.Schema
    
    var personSchema = Schema({
      _id     : Number,
      name    : String,
      age     : Number,
      stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
    });
    
    var storySchema = Schema({
      _creator : { type: Number, ref: 'Person' },
      title    : String,
      fans     : [{ type: Number, ref: 'Person' }]
    });
    

    Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single document, multiple documents, plain object, multiple plain objects, or all objects returned from a query. Let's look at some examples.

    Better you can get more information please visit :http://mongoosejs.com/docs/populate.html

    0 讨论(0)
  • 2020-11-21 05:55

    I created this quizz as reference to know if you should use one or another

    http://indie-rok.github.io/embedded-vs-reference-mongo-db

    0 讨论(0)
提交回复
热议问题