简体   繁体   English

如何在引用另一个模式的猫鼬模式中填充 aa 字段,并且该模式进一步引用另一个模式?

[英]How to poulate a a field in a mongoose schema which is refrenced to another schema , and this schema is further refrenced to another schema?

The field (viewed_posts) i want to populate in User Schema:我想在用户架构中填充的字段 (viewed_posts):

viewed_posts: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Viewed"
    }
  ]

Viewed Schema :查看架构:

var viewedSchema = new mongoose.Schema({

    hitsByUser: {type: Number, default: 0},
    viewsByUser: {type: Number, default: 0},
    post: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Post"
    }
  
});

Post Schema :发布架构:

var mongoose = require('mongoose');

var passportLocalMongoose = require("passport-local-mongoose");
var uniqueValidator = require('mongoose-unique-validator');
var marked = require('marked');
var slugify = require('slugify');
// this is done for sanitizing html so that user cannot write a script in the input
const createDomPurify = require('dompurify')
const {JSDOM} = require('jsdom')
const dompurify = createDomPurify(new JSDOM().window)


var postSchema = new mongoose.Schema({
  postNumber: Number,
  title: String,
  content: String,
  subject: String, // currently we have 4 subjects so one out of 4 subjects will be stored here
  likes: {type:Number,default:0},
  // likes: {
  //   id:{
  //     type: mongoose.Schema.Types.ObjectId,
  //     ref: "User"
  //   }
  // },
  views: {type:Number,default:0},
  actualViews: {type:Number,default:0},
  shares: Number,
  isReviewedByAdmin: {type: Boolean, default: false},
  isReviewedByAuditor: {type: Boolean, default: false},
  author: {
    id:{
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    },
    username: {
      type: String
    }
  },
  publish_date: {
    type: String, 
    default: Date.now
  },
  publishDay: String,
  slug: {
    type: String,
    required: true,
    
  },
  sanitizedHtml: {
    type: String,
    required: true
  },
  
  imagename: String                                        //2002-12-09
});

I wish to see whole structure printed , but i can only populate viewed_posts, how can i populate "post" which is inside viewed Schema and see here:我希望看到整个结构打印,但我只能填充viewed_posts,我怎样才能填充查看架构内的“post”并在这里查看:

User.findById(req.user._id).populate("viewed_posts").exec((err,ans)=>{
                  if(err) console.log(err)
                  else{
                    
                    
                    console.log("this is the answer ",ans)
                 }})

The output i get:我得到的输出:

 },
    {
      hitsByUser: 0,
      viewsByUser: 0,
      _id: 5f9e85aeec37700f54a4d029,
      post: 5f9a93d38d7cf8544ce9cc21,
      __v: 0
    },
    {
      hitsByUser: 0,
      viewsByUser: 0,
      _id: 5f9e85d61841000478c85f8a,
      post: 5f82773f1998150024d4c8fc,
      __v: 0
    },

But i expect this post to be expanded too, instead of just showing id , How can i achieve it.但我希望这篇文章也能得到扩展,而不仅仅是显示 id ,我该如何实现它。 Any Help Would be appreciated.任何帮助,将不胜感激。

Mongoose supports nested populating (see in the docs: https://mongoosejs.com/docs/populate.html#deep-populate ). Mongoose 支持嵌套填充(参见文档: https : //mongoosejs.com/docs/populate.html#deep-populate )。 Note that you have to specify your model name of post schema where I´ve put the "post-model-name" placeholder.请注意,您必须在我放置“post-model-name”占位符的位置指定 post schema的模型名称

So you could try something like this:所以你可以尝试这样的事情:

User.findById(req.user._id)
  .populate({ 
     path: 'viewed_posts',
     populate: {
       path: 'post',
       model: 'post-model-name'
     } 
  })
  .exec();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM