Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose: populate() / DBref or data duplication?

I have two collections:

  1. Users
  2. Uploads


Each upload has a User associated with it and I need to know their details when an Upload is viewed. Is it best practice to duplicate this data inside the the Uploads record, or use populate() to pull in these details from the Users collection referenced by _id?


OPTION 1

var UploadSchema = new Schema({
    _id: { type: Schema.ObjectId },
    _user: { type: Schema.ObjectId, ref: 'users'},
    title: { type: String },
});


OPTION 2

var UploadSchema = new Schema({
    _id: { type: Schema.ObjectId },
    user: { 
           name: { type: String },
           email: { type: String },
           avatar: { type: String },
           //...etc
          },
    title: { type: String },
});


With 'Option 2' if any of the data in the Users collection changes I will have to update this across all associated Upload records. With 'Option 1' on the other hand I can just chill out and let populate() ensure the latest User data is always shown.

Is the overhead of using populate() significant? What is the best practice in this common scenario?

like image 245
wilsonpage Avatar asked Nov 01 '11 17:11

wilsonpage


People also ask

What does populate in Mongoose do?

Mongoose Populate() Method. In MongoDB, Population is the process of replacing the specified path in the document of one collection with the actual document from the other collection.

What is the difference between schema and model in Mongoose?

A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

Why schema is used in Mongoose?

Schemas not only define the structure of your document and casting of properties, they also define document instance methods, static Model methods, compound indexes, and document lifecycle hooks called middleware.

What does REF do in Mongoose schema?

The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _id s we store here must be document _id s from the Story model. Note: ObjectId , Number , String , and Buffer are valid for use as refs.


1 Answers

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*

like image 192
kilianc Avatar answered Oct 20 '22 22:10

kilianc