Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modelling blogs and ratings in mongodb and nodejs

I have a blogs collection that contains title, body and agrregate rating that the users have given to them. Another collection 'Ratings' whose schema has reference to the blog, user who rated(if at all he rates them) it in the form of their ObjectIds and the rating they have given ie., +1 or -1.

When a particular user browses through blogs in the 'latest first' order (say 40 of them per page. Call them an array of blogs[0] to blogs[39]) I have to retrieve the rating documents related to this particular user and those 40 blogs if at all the user rated them and notify him of what ratings he has given those blogs.

I tried to extract all rating documents of a particular user in which blog reference objectIds lie between blogs[0]._id and blogs[39]._id which returns empty list in my case. May be objectIds cant be compared using $lt and $gt queries. In that case how should I go about it? Should I redesign my schemas to fit to this scenario?

I am using mongoosejs driver for this case. Here are the relevant parts of the code which differ a bit in execution but youu get the idea.

Schemas:

Client= new mongoose.Schema({
    ip:String
})

Rates = new mongoose.Schema({
    client:ObjectId,
    newsid:ObjectId,
    rate:Number
})

News = new mongoose.Schema({
  title: String,
  body: String,
  likes:{type:Number,default:0},
  dislikes:{type:Number,default:0},
  created:Date,
  // tag:String,
  client:ObjectId,
  tag:String,
  ff:{type:Number,default:20}
});

models:

var newsm=mongoose.model('News', News);
var clientm=mongoose.model('Client', Client);
var ratesm=mongoose.model('Rates', Rates);

Logic:

newsm.find({tag:tag[req.params.tag_id]},[],{ sort:{created:-1},limit: buffer+1 },function(err,news){

ratesm.find({client:client._id,newsid:{$lte:news[0]._id,$gte:news.slice(-1)[0]._id}},function(err,ratings){
 })
})

Edit: While implementing the below said schema, I had to do this query in mongoose.js

> db.blogposts.findOne()
{ title : "My First Post", author: "Jane",
  comments : [{ by: "Abe", text: "First" },
              { by : "Ada", text : "Good post" } ]
}
> db.blogposts.find( { "comments.by" : "Ada" } )

How do I do this query in mongoose?

like image 614
Kamal Reddy Avatar asked May 18 '12 06:05

Kamal Reddy


1 Answers

A good practice with MongoDB (and other non-relational data stores) is to model your data so it is easy to use/query in your application. In your case, you might consider denormalizing the structure a bit and store the rating right in the blog collection, so a blog might look something like this:

{
  title: "My New Post",
  body: "Here's my new post. It is great. ...",
  likes: 20,
  dislikes: 5,
  ...
  rates: [
    { client_id: (id of client), rate: 5 },
    { client_id: (id of another client), rate: 3 },
    { client_id: (id of a third client), rate: 10 }
  ]
}

The idea being that the objects in the rates array contains all the data you'll need to display the blog entry, complete with ratings, right in the single document. If you also need to query the rates in another way (e.g. find all the ratings made by user X), and the site is read-heavy, you may consider also storing the data in a Rates collection as you're doing now. Sure, the data is in two places, and it's harder to update, but it may be an overall win after you analyze your app and how it accesses your data.

Note that you can apply indexes deep into a document's structure, so for example you can index News.rates.client_id, and then you can quickly find any documents in the News collection that a particular user has rated.

like image 185
Michelle Tilley Avatar answered Oct 15 '22 11:10

Michelle Tilley