Modeling blogs and ratings in mongodb and nodejs

I have a collection of blogs that contains the title, body and agrregate ratings that users gave them. Another collection is โ€œRatingsโ€, whose scheme has a link to a blog, a user who rated (if at all evaluates them) in the form of their ObjectIds and the rating they gave, i.e. +1 or -1.

When a particular user views blogs in the "last first" order (say 40 of them per page. Call them an array from blogs[0] to blogs[39] ). I need to get rating documents related to this particular user and those 40 blogs, if at all the user has rated them and informed him what ratings he gave to these blogs.

I tried to extract all the rating documents of a user in which the link objects of the reference object are between blogs[0]._id and blogs[39]._id , which returns an empty list in my case. It may not be possible to map object objects using the $lt and $gt queries. In that case, how do I do this? Should I redesign my circuits according to this scenario?

I am using the mongoosejs driver for this case. Here are the relevant parts of the code that are slightly different from the execution, but you get this 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); 

Logics:

 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: Following the diagram below, I had to execute this request in the mongoose.js file

 > 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 to make this request in mongoose?

+6
source share
1 answer

Good practice with MongoDB (and other non-relational data warehouses) is to model your data, so it is easy to use / query in your application. In your case, you can consider denormalizing the structure a bit and save the rating directly on the blog, so the blog might look something like this:

 { title: "My New Post", body: "Here 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 is that the objects in the rates array contain all the data necessary to display the blog entry, complete with ratings, directly in one document. If you also need to request bids in a different way (for example, to find all the ratings made by user X), and the site will be hard to read, you can also consider storing the data in the rates collection, as you are doing now. Sure, the data is in two locations and theyโ€™re more difficult to update, but it can be a common victory after analyzing your application and how it accesses your data.

Please note that you can apply indexes deep in the structure of the document, 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.

+4
source

Source: https://habr.com/ru/post/916066/


All Articles