Embedded or referenced relations

I use mongodb and mongoid gem and I'd like to get some advice.

I have an app where User has many Markets and Market has many Products. I need to search for the products, say in a specific price range, in all (or any) the markets which belong to the user.

Which relation fits better for this, embedded or referenced?

I currently use referenced and it looks like so

class User has_many :markets end class Market belongs_to :user has_many :products end class Product belongs_to :calendar belongs_to :user end

And for search, I use this query

Product.where(user_id: current_user.id). in(market_id: marked_ids). where(:price.gte => price)

I'm curious, since mongdb is a document oriented database, would I benefit in a performance or design, if I used embedded documents in this situation?


In your case I would advice to use referenced data. Because I suppose that you need to manipulate each of those collections on it's own (you need to be able to edit/delete/update "products" by _id, and do some other complicated queries, which is much easier and effective when you have separate collection).

At the same time I would store some full embedded data in Users collection, just for speed-up display to visitor's browser. Let's say you have a user's page where you want to show user's profile and top-5 markets and top-20 products. You can embed those newest top-5 and top-20 to User's document and update those embedded objects when there are new markets/products. In this case - when you show user's page you need to make just 1 query to MongoDB. So this works as cache. If visitor needs to view more products, he goes to the next page "Products" and query separate "Products" collection in MongoDB.


Use embedded documents if you only need to access the item through the parent class. If you need to query it directly or from multiple objects, use a reference.


  • php session not working after publish on web hosting
  • Easiest way to parse date in String format to GregorianCalendar
  • In java, what time is the start of the day?
  • JSpinner time range + HH:MM format
  • How to get the exact value from the array - php
  • Why do attr_accessor virtual attributes not get outputted when you do record.inspect
  • How to finish activity when date picker dialog on cancel button is clicked
  • Disabling previous date, month, year JTAppleCalendar in iOS using Swift 3.0
  • How to add initial data to angularjs app from rendered html
  • Parsing date string (MM-dd) to java date in default year
  • How to make JDatePicker text field formatted for input?
  • How to filter results with Ransack
  • Home Brew PHP 7.2.5 Install with cURL
  • cakephp select options attributes
  • How do I retrieve Google Calendar Resources for my company using the PHP API client?
  • Passing FullCalendar date to my dialog box modal form
  • How to get dates/appointments from app into windows phone calender?
  • Yii Booster datepicker not working correctly
  • Sorting by year and monthname
  • Calendar control for iPad [duplicate]
  • current user id saved from _form or controller
  • How can I retrieve JSONdata from ASP.NET and send it to jQuery? (implementing FullCalendar)
  • Get All Google Calendars Shared Within A Domain
  • Embedded or referenced relations
  • Multiple calender in exchange web service
  • How to mock current date?
  • Is it possible to disable certain weekdays in DatePickerDialog?
  • How do I write an item to a DynamoDb with the AWS DynamoDB DocumentClient?
  • How to get month name with year and list of years between two Date
  • How to access recipient on sent messages page with mailboxer
  • How to Add Polymorphic Comments to Feed?
  • User messaging system
  • Find Previous month name using Calender or any classes that in java
  • Laravel: Getting Session ID oddly truncates when using foreach
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?