在mongodb和nodejs中建模博客和评级 [英] Modelling blogs and ratings in mongodb and nodejs

查看:80
本文介绍了在mongodb和nodejs中建模博客和评级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个博客集合,其中包含用户给他们的标题,正文和综合评分.另一个集合"Ratings",其架构已引用博客,该用户以其ObjectId和给出的等级(即+1或-1)对其进行了评级(如果对他进行了评级).

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.

当特定用户以最新优先"顺序浏览博客时(例如每页40个.将它们称为blogs[0]blogs[39]的数组),我必须检索与此特定用户相关的评级文档以及这40个博客(如果用户对他们进行了评分),并通知他他对这些博客的评价是什么.

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.

我试图提取特定用户的所有评级文档,其中博客引用objectIds位于blogs[0]._idblogs[39]._id之间,在我的情况下,该文档返回空列表.可能是无法使用$lt$gt查询比较objectIds.在那种情况下,我应该怎么做?我应该重新设计架构以适合这种情况吗?

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?

在这种情况下,我正在使用mongoosejs驱动程序.以下是代码的相关部分,它们在执行上有所不同,但您可以理解.

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.

模式:

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}
});

型号:

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

逻辑:

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){
 })
})

在实现以下所述架构时,我不得不在mongoose.js中进行此查询

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?

推荐答案

MongoDB(和其他非关系数据存储)的一个好习惯是对数据建模,以便在应用程序中轻松使用/查询.在您的情况下,您可能会考虑对结构进行一定程度的非规范化并将评级直接存储在博客集中,因此博客可能看起来像这样:

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 }
  ]
}

这样的想法是,rates数组中的对象包含在单个文档中显示博客条目所需的所有数据,包括等级.如果您还需要以其他方式查询费率(例如,找到用户X做出的所有费率),并且该站点的阅读量很大,则可以考虑将数据存储在Rates像现在一样收藏.当然,数据在两个地方,并且很难更新,但是在您分析应用程序以及数据访问方式之后,这可能是一个整体胜利.

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.

请注意,您可以将索引应用于文档结构的深处,例如,您可以为News.rates.client_id建立索引,然后可以快速找到News集合中特定用户已评级的任何文档.

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.

这篇关于在mongodb和nodejs中建模博客和评级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆