Mongoose: populate() / DBref or data duplication?

后端 未结 2 350
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 07:55

I have two collections:

  1. Users
  2. Uploads


Each upload has a User associated with it and I need to know their

相关标签:
2条回答
  • 2021-02-01 08:45

    If You need to query on your Users, keep users alone. If You need to query on your uploads, keep uploads alone.

    Another question you should ask yourself is: Every time i need this data, do I need the embedded objects (and vice-versa)? How many time this data will be updated? How many times this data will be read?

    Think about a friendship request: Each time you need the request you need the user which made the request, then embed the request inside the user document.

    You will be able to create an index on the embedded object too, and your search will be mono query / fast / consistent.


    Just a link to my previous reply on a similar question: Mongo DB relations between objects

    I think this post will be right for you http://www.mongodb.org/display/DOCS/Schema+Design

    Use Cases

    Customer / Order / Order Line-Item

    Orders should be a collection. customers a collection. line-items should be an array of line-items embedded in the order object.

    Blogging system.

    Posts should be a collection. post author might be a separate collection, or simply a field within posts if only an email address. comments should be embedded objects within a post for performance.

    Schema Design Basics

    Kyle Banker, 10gen

    http://www.10gen.com/presentation/mongosf2011/schemabasics

    Indexing & Query Optimization Alvin Richards, Senior Director of Enterprise Engineering

    http://www.10gen.com/presentation/mongosf-2011/mongodb-indexing-query-optimization

    **These 2 videos are the bests on mongoddb ever seen imho*

    0 讨论(0)
  • 2021-02-01 08:45

    Populate() is just a query. So the overhead is whatever the query is, which is a find() on your model. Also, best practice for MongoDB is to embed what you can. It will result in a faster query. It sounds like you'd be duplicating a ton of data though, which puts relations(linking) at a good spot.

    "Linking" is just putting an ObjectId in a field from another model.

    Here is the Mongo Best Practices http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-SummaryofBestPractices

    Linking/DBRefs http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-SimpleDirect%2FManualLinking

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