如何在猫鼬中从活动收集中获取照片收集数据?

Posted

技术标签:

【中文标题】如何在猫鼬中从活动收集中获取照片收集数据?【英文标题】:How to bring photos collection data from campaign collection in mongoose? 【发布时间】:2020-08-25 02:24:04 【问题描述】:
I have 3 collections in mongoose. one is campaign collection that reference to user and other one is photo collection that reference to campaign collection. I can pull the campaigns data based on user but not able fetch photos based on campaigns. Here One user might have many campaigns and each campaign has their own photo collections.

 My models :

**Photo collection:**

var photoSchema = new Schema(
    Name: type: String,

    Email:   type: String ,

    Photo:   type: String ,
    Description:type:String,

    PhoneNumber:type:String,
    CampaignId:  
      type: Schema.Types.ObjectId, 
      ref: 'Campaigns', 
     

  );

这是引用用户的活动集合。这再次从照片收藏中获得参考。我想在画廊中呈现与一项活动相关的照片。广告系列标题作为 URL 中的参数发送,但无法弄清楚如何发送。

照片收藏的发布路线:

exports.postEntriesCollection = (req, res) =>

Campaign.findOne(Title:req.params.Title, function(err, campaign) 

let filename = '';


if(!isEmpty(req.files)) 
    let file = req.files.file;
    filename = file.name;
    let uploadDir = './public/PhotoContestUploads/uploads/';
    console.log(filename);
    file.mv(uploadDir+filename, (err) => 
        if (err)
            throw err;
    );



const photo = new PhotoEntries(

  Email:req.body.email,
  Name:req.body.name,
  Description:req.body.description,
  Phonenumber:req.body.Phone, 
  CampaignId:campaign._id,
  Photo:`uploads/$filename`
);
 photo.save().then(post => 

  req.flash('success',  msg: 'Uploaded successfully' );
     res.redirect('#');

 );
);

**campaign collection:**

var campaignSchema = new Schema(
    Campname: type: String,
     userId:  
      type: Schema.Types.ObjectId, 
      ref: 'User', 
     

  );


****To render gallery my route like this :**  I try to pull photos using campaign ID stored in photos collection here :** 

exports.getGallery = (req, res) => 
 PhotoEntries.find(req.CampaignId, ['Photo'], sort: _id: -1 , function(err, photos) 
    res.render('admin/gallery',   photolist : photos ); 
  );



I am not able to get gallery of particular campaign instead i am getting all photos relating all campaigns. I want to show only gallery of particular campaign. I have gallery url like this :  /photocontestapp/awesome%20baby%20srija/gallery.How can i achieve this.  

【问题讨论】:

我假设PhotoEntiesphotoSchema 的模型,req.CampaignId 应该是CampaignId: req.CampaignId,对吧? 是的,正确的@TheeSritabtim 在这种情况下,您应该检查 req.CampaignId 不是 undefined 并且是架构中指定的 ObjectId 类型。 好吧,我将删除它,我如何获取特定活动的画廊照片 我正在将广告系列 ID 保存到照片架构中以供参考 【参考方案1】:

在 cmets 中,我假设您尚未设置 req.CampaignId,因此您应该先查询它并找到该广告系列。这应该可以解决您的问题。

exports.getGallery = (req, res) => 
  Campaign.findOne( Title: req.params.Title , function(err, campaign) 
    PhotoEntries.find( CampaignId: campaign._id , ['Photo'], sort: _id: -1 , function(err, photos) 
      res.render('admin/gallery',   photolist : photos ); 
    );
  );
;

【讨论】:

以上是关于如何在猫鼬中从活动收集中获取照片收集数据?的主要内容,如果未能解决你的问题,请参考以下文章

当我想在猫鼬中从数据库中查找文档时,函数返回未定义 [重复]

如何在猫鼬中为 geojson 数据创建模式?

如何仅在猫鼬中获取子文档?

如何仅在猫鼬中获取子文档?

如何在猫鼬中填充模型

如何在猫鼬中进行原始 mongodb 操作?