简体   繁体   English

发布Mongoose复杂模式

[英]Posting Mongoose Complex Schema

Hello there 你好

I've got this Mongoose Schema (I know there is something wrong there), the important bit is the " region " part of it. 我有这个Mongoose Schema(我知道那里有问题),重要的是它的“ 区域 ”部分。

    const mongoose = require('mongoose');

    const destination = new mongoose.Schema({
      name: { type: String, required: true }, 
      flag: String,
      creationDate: { type: Date, default: Date.now },
      region: { id: { type: mongoose.Schema.Types.ObjectId, ref: 'Region' }, name: String },
}, {strict: true});

module.exports = mongoose.model('Destination', destination);

I'm posting using this form: 我正在使用此表单发布:

 <form action="/destinations" method="post"> <div class="form-group"> <label for="destination[name]">Name</label> <input class="form-control" type="text" name="destination[name]" placeholder="Destination name..."> </div> <div class="form-group"> <label for="destination[name]">Flag</label> <input class="form-control" type="text" name="destination[flag]" placeholder="Destination flag..."> </div> <div class="form-group"> <label for="destination[region]">Region</label> <select class="form-control mb-4" name="region[name]]"> <option selected disabled hidden>Choose a region</option> <% allRegions.forEach(region => { %> <option value="<%= region.name %>"> <%= region.name %> </option> <% }); %> </select> </div> <button class="btn btn-primary btn-block" type="submit">Add</button> </form> 

All data is being correctly posted to the Node.js file that processes it, however I can't find a way to save the "region id", this is how is being processed right now: 所有数据都正确地发布到处理它的Node.js文件,但是我找不到保存“区域ID”的方法,这就是现在正在处理的方式:

exports.destinations_create = (req, res) => {
  req.body.destination.region = { name: req.body.region.name };
  Destination.create(req.body.destination)
  .then(newDestination => {
    Regions.findOne({name: req.body.region.name})
    .then(foundRegion => {
      newDestination.region.id = foundRegion._id;
      foundRegion.countries.push(newDestination._id);
      foundRegion.save();
    });
    res.redirect('/destinations');
  })
  .catch(err => res.redirect('/'));
}

I thought I could leave the id empty and then add it later, as it is just an object, but nothing works, any ideas of what is wrong here? 我以为我可以将id留空,然后再添加它,因为它只是一个对象,但没有任何作用,这里有什么错误的想法?

Thanks in advance! 提前致谢!

You could leave id as well as name property of region since you are referencing Region. 您可以保留id以及区域的name属性,因为您正在引用Region。

 const mongoose = require('mongoose');

     const destination = new mongoose.Schema({
      name: { type: String, required: true }, 
      flag: String,
      creationDate: { type: Date, default: Date.now },
      region: {type: mongoose.Schema.Types.ObjectId, ref: 'Region' });
      module.exports = mongoose.model('Destination', destination);

and then in your add method: 然后在你的添加方法中:

     exports.destination_create = async(req,res)=>{
    const region = await Region.findOne({name: req.body.region});
    const destination = new Destination({
     region: region._id,
    name: req.body.name,
    flag: req.body.flag
})
await destination.save();
res.redirect('/destination');
 }

I kinda found a solution, and I say kinda because I am not sure about how efficient it is memory wise but: 我有点找到解决方案,之所以这样说是因为我不确定它在内存方面的效率如何,但是:

Model destination was changed: 模型目的地已更改:

region: { type: mongoose.Schema.Types.ObjectId, ref: 'Region' },

And then what I did was to " deep populate " the regions query: 然后我做的是“ 深入填充 ”区域查询:

Regions.find({})
  .select('_id name image countries')
  .populate({
    path: 'countries',
    model: 'Destination',
    populate: {
      path: 'cities',
      model: 'City'
    }
  }).then(...).catch(...);

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

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