Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoid Map Reduce with has_one
    primarykey
    data
    text
    <p>I am using Mongoid and MongoDB in an application I am making. I have a user that has one profile like so:</p> <pre><code>class User field :email, :type =&gt; String field :name, :type =&gt; String field :date_of_birth, :type =&gt; DateTime has_one :profile end class Profile field :votes, :type =&gt; Hash field :biography, :type =&gt; String belongs_to :profile end </code></pre> <p>The votes hash is structured like so:</p> <pre><code>profile : { "user_id" : ObjectId("511b76b0e80c505750000031"), "votes": { "vote_count": 3, "up_votes": 3, "down_votes": 0 } } </code></pre> <p>I am running map reduce like so:</p> <pre><code>map = " function () { values = { name: this.name } emit(this._id, values); } " reduce = " function (key, emits) { return emits; } " User.map_reduce(map, reduce).out(replace: "leaderboards").each do |document| ap document end </code></pre> <p>This works fine and creates a new collection in Mongo called leaderboards. However I am trying to map some data from the profile so it includes the vote_count field from the profile.</p> <p>Essentially making my map function look like so:</p> <pre><code>map = " function () { values = { name: this.name, votes: this.profile.votes.vote_count } emit(this._id, values); } " </code></pre> <p>However I am have difficulty grabbing the profile associated with the user. Does anyone know how I can pull the data from the users profile?</p> <p>If something isn't clear here let me know. Any help would be appreciated.</p> <p>Tony</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload