本文介绍了Mongodb模式定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码新闻/媒体网站,我需要一个新闻"部分,一个评论"部分,一个趋势"部分,结合了前面的部分,就像这里:我为新闻"制作了一个模式,为评论"制作了一个模式.如何创建趋势"部分(如电影"部分上方的图像)?代码 :在app.js中,

Coding a news/media website, I want a "News" section, "Reviews" section, a"Trending" section, which combines both the previous sections, just like here:I have made one schema for "News", one for "Reviews".How can I make a "Trending" section(as in the image above "Movies" section)?Code : In app.js,

 //LANDING PAGE
    app.get('/', function (req, res,next) {
      Blogdemo.find({}).sort([['_id', -1]]).limit(3).exec(function(err,allBlogs) { //finds latest posts for 1st Schema (upto 3)
            if(err) {    
                console.log(err);
                next();
            } else {
                res.locals.blog = allBlogs;
                // res.render("landing", {blog : allBlogs , moment : now});
                next();
            }

        })

}, function (req, res) {
  Review.find({}).sort([['_id', -1]]).limit(3).exec(function(err,allReviews) { //finds latest posts of 2nd Schema
                if(err) {
                    console.log(err);
                } else {
                    res.locals.review = allReviews;
                    res.render("landing", res.locals);
                }   
    })
}) 

在review.js中,

In review.js ,

 var mongoose = require("mongoose");

//SCHEMA SETUP
var reviewSchema = new mongoose.Schema({
    image : String,
    title : String,
    body : String,
    rating : String,
    created : {type : Date, default : Date.now()},
    comments: [
      {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Comment" //name of the model
      }
   ]
})

module.exports = mongoose.model("review", reviewSchema);

新闻"模式几乎相同(无评论).我定义架构的方式是否错误?如果没有,那么我该如何构建趋势"部分?

The "News" schema is almost the same(no review).Is my way of defining schema wrong? If not, then how can I build the "Trending" section?

是否有任何mongodb方法可以从新闻"和评论"中查找最新帖子以构建趋势"部分(就像第一张图片一样)?

Is there any mongodb method which can find the latest posts from "News" and "Reviews" to build the "Trending" section(just like in 1st picture)?

推荐答案

从我的代码中可以看到,当前的NewsReview模式看起来不错.

From what i can see from your code, your current News and Review Schema looks fine.

您需要为Trending定义另一个Schema.

var TrendingSchema = new mongoose.Schema({
    referenceId : {
        type : mongoose.Schema.Types.ObjectId
    },
    postType : String //To store News or Reviews
});

在保存新的NewsReviews的同时,将新保存的文档的_id插入trending集合中.

While saving new News or Reviews, insert the _id of newly saved document in the trending collection.

var news = new News();
news.image = newsImage;
...
news.save(function(err,result)
{
    if(!err)
    {
        var trending =  new Trending();
        trending.referenceId = result._id;
        trending.postType = "News";
        treding.save(function(err)
        {
            if(!err)
            {
                //success response
            }
            else
            {
                //error  response
            }
        });
    }
    else
    {
        //send error response
    }
});

类似,同时保存评论帖子

Similarly while saving Review Post

var review = new Review();
review.image = reviewImage;
...
review.save(function(err,result)
{
    if(!err)
    {
        var trending =  new Trending();
        trending.referenceId = result._id;
        trending.postType = "review"
        treding.save(function(err)
        {
            if(!err)
            {
                //success response
            }
            else
            {
                //error  response
            }
        });
    }
    else
    {
        //send error response
    }
});

因此,现在Trending集合将按created的顺序包含新保存的NewsReview.这样您就可以获取新的ReviewNews帖子.

Thus now Trending Collection will contain, newly saved News or Review, in the order they are created. Thus you will be able to get new Review or News Post.

在获取Trending时,您可以根据postType使用NewsReview Schema populate对其进行populate.

While fetching Trending, you can populate them using News or Review Schema based on the postType.

Trendign.find({}).limit(10).exec(function(err,result)
{
    if(!err && result.length!=0)
    {
        var trendingPosts = [];
        result.forEach(function(trending){
            if(trending.postType === "News"){
                trending.populate({path : 'referenceId',model : 'News'},function(err,populatedItem)
                {
                    if(!err)
                    {
                        trendingPosts.push(populatedItem);
                    }
                });
            }
            else if(trending.postType === "Review"){
                trending.populate({path : 'referenceId',model : 'Review'},function(err,populatedItem)
                {
                    if(!err)
                    {
                        trendingPosts.push(populatedItem);
                    }
                });
            }
        });
        //now send the trendingPost array with latest News and Review Posts
    }
    else
    {
        //send Error response
    }
});

现在,您可以显示最新的NewsReview并输入类型postType.

Now you can show the latest News or Review and write the type postType.

希望这就是您想要的.

这篇关于Mongodb模式定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:34