Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8708851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:13:56+00:00 2026-06-13T04:13:56+00:00

I have a schema for an item and a user. I want to allow

  • 0

I have a schema for an item and a user. I want to allow a user to like or dislike an item and I also want to store a ratings association on the item as well as the user.

var UserSchema = new Schema({
    username    : { type: String, required: true, index: { unique: true }
   , likes : [{ type: Schema.ObjectId, ref: 'Item'}]
   , dislikes : [{ type: Schema.ObjectId, ref: 'Item'}]
   , ratings: [???]
});

var ItemSchema = new Schema({
    name: { type: String},
   , likes : [{ type: Schema.ObjectId, ref: 'User'}]
   , dislikes : [{ type: Schema.ObjectId, ref: 'User'}]
   , ratings: [???]
});

The user stores an item ref, and the item stores a user ref for likes/dislikes. I am not sure how to store ratings for as an attribute though, since I want both the user and the value they rated the item.

item.ratings.forEach(function(rating){
  <%= rating.user.username %> gave <%= item.name %> a <%= rating.value %>.
});

I also want to get a list of items a user has rated along with the rating value:

user.ratings.forEach(function(rating){
  <%= user.username %> has rated <%= rating.item.name %> and gave it a <%= rating.value %>
});

What should my “ratings” schema look like? Is it possible to store two values? a user object id and a rating value (integer) and have a collection of these?

The other problem I see with my method is that mongoose doesn’t support deep populate yet, so I would have to either use a module for it (https://github.com/JoshuaGross/mongoose-subpopulate), that is largely un-tested or store it in a different manner that won’t have more than one level of nesting, so I can get my data with .populate()

Any feedback is appreciated, as I’m new to noSQL and perhaps I’m overcomplicating this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:13:58+00:00Added an answer on June 13, 2026 at 4:13 am

    I would do as you said. I would use a Rating schema:

    var RatingSchema = new Schema({
       , _user : { type: ObjectId, ref: 'User' }
       , _item : { type: ObjectId, ref: 'Item' }
       , value : Integer
    });
    

    If you want to be able to access the ratings from the User schema, you would have to add a hook so that any saved RatingSchema is added to user.ratings.

    var UserSchema = new Schema({
      /* ... */
      ratings: [{ type: Schema.ObjectId, ref: 'Rating'}]
    });
    
    RatingSchema.post('save', function () {
      // push this.id to this._user.ratings
      // save this._user
    });
    

    Regarding, “The other problem I see with my method is that mongoose doesn’t support deep populate yet”, if you don’t want to use the mongoose-subpopulate hack, I suggest you refactor the loading of your models into static methods. For example:

    UserSchema.statics.findByIdAndDeepPopulate = function (i, cb) {
      UserSchema.findOne(id)
        .exec(function(err, user) {
          if (err || !user) return cb(new Error('User not found'));
          if (user._ratings.length == 0) return cb(null, user);
    
          // Load and populate every ratings in user._ratings
          for(var i = 0; i < user._ratings.length; i++) {
            function(i) {
              RatingSchema
                .populate('_item')
                .exec(err, function(rating) {
                  user._ratings[i] = rating;
                  if (i == user._ratings.length) return cb(null, user);
                });
            }(i);
          }
        });
    }
    

    EDIT: Now that I think about it again, why not simply store the ratings as an embed document in UserSchema?

    var UserSchema = new Schema({
      /* ... */
      ratings: [ RatingSchema ]
    });
    

    You could then simply populate this way:

    UserSchema.findOne(id)
      .populate('ratings._item')
      .exec(function(err, user) {
        if (err || !user) return next(new Error('User not found'));
    
        console.log(user.ratings);
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a schema design that looks like this: Table: User Row Column Family
I have a table which have schema like this id name 1 jack 2
I have a schema like this <h1> 5/2009 <br/> Question: This is the question
I want to hide a menuItem regarding user's rights. The menu item is placed
I currently have this XML schema : <PSC5> <POI_ORI> <CIT>LIM</CIT> </POI_ORI> </PSC5> if user
I have the following generic schema to represent different types of information. var Record
I have a WPF listbox with custom items. Each item is a user control
I have to call mapactivity from list activity.whenever user click on item on listview,
I have a transition looking like this: <?xml version=1.0 encoding=UTF-8?> <transition xmlns:android=http://schemas.android.com/apk/res/android > <item
I have an options_menu.xml file like this: <?xml version=1.0 encoding=utf-8?> <menu xmlns:android=http://schemas.android.com/apk/res/android> <item android:id=@+id/search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.