Mongoose多个对象数组的嵌套模式(schema)

4
我正在尝试使用 Node、Express 和 MongoDB 为 Web 应用程序构建 REST API,在前端上有三个路由或部分:
  1. 名言
  2. 故事
  3. 新闻
当用户点击名言时,他将只看到名言;同样的情况也适用于故事和新闻。
我不确定如何为这种类型的 API 构建模式。这是我的方法:首先,我为每个部分单独构建模式,然后将它们全部合并到主 ApiSchema 中。我不确定这种方法是否正确。
  const mongoose = require('mongoose')
  const Schema = mongoose.Schema

  const QuotesSchema = new Schema({
     author: {
        type: String,
        required: true
     },
     categories: {
        type: [String],
        required: true
     }
  })

  module.exports = Quotes = mongoose.model('quotes', QuotesSchema)

  const StoriesSchema = new Schema({
     title: {
        type: String,
        required: true
     },
     description: {
        type: String,
        required: true
     }
  })

  module.exports = Stories = mongoose.model('stories', StoriesSchema)

  const NewsSchema = new Schema({
     headline: {
        type: String,
        required: true
     }
     date: {
        type: Date,
        default: Date.now
     }
  })

 module.exports = News = mongoose.model('news', NewsSchema)

 const Quotes = require('./Quotes')
 const Stories = require('./Stories')
 const News = require('./News')

 const ApiSchema = new Schema({
     quotes: [Quotes],
     Stories: [Stories],
     News: [News]
  })


  module.exports = Api = mongoose.model('api', ApiSchema)

我希望我的 API 像以下示例一样

  [
     {
        quotes: [
           { author: 'John', quote: 'be happy', tags: ['life', 'heart'] },
           { author: 'Mark', quote: 'be creative', tags: ['motivation', 'education'] }
        ]
     },
     {
        story: [
           { title: 'broken heart', description: 'sit ae Ipsa, laboriosam!', category: ['lorem1', 'lorem2'] },
           { title: 'lorem ipsum', description: 'Lorem ipsum dolor sit.', category: ['lorem1', 'lorem2'] }
        ]
     },
     {
        news: [
           { headline: 'ipsum Lorem', city: 'Jehigi' },
           { headline: 'Lorem, ipsum', city: 'Fahmori' }
        ]
     }
  ]

我希望你能明确理解我的意思,如果你能帮助我就太好了。


因为我想把所有的API数据放在一个地方。 - Basit
你打算如何使用 ApiSchema?用于什么目的? - Molda
这些模式与任何用户或其他内容相关联吗? - Vivek Ghanchi
你能分享一下这些的输出吗? - Vivek Ghanchi
请提供英文内容以供翻译。 - Basit
显示剩余2条评论
1个回答

2
我认为你走在了正确的道路上,但是你应该使用引用。
(1) 拥有四个单独的模型和四个单独的架构是一个好主意,但是我建议将它们分别放在不同的文件中。
(2) 在你的APISchema中,你可以像这样引用其他的架构:
const ApiSchema = new Schema({
  quotes: [{ type: mongoose.Schema.Types.ObjectId,, ref: 'quotes' }]
  stories: [{ type: mongoose.Schema.Types.ObjectId,, ref: 'stories' }],
  news: [{ type: mongoose.Schema.Types.ObjectId,, ref: 'news' }]
});

(3) 现在当你创建API文档时,必须掌握报价、故事和新闻的_id。如果你有这些信息,那么接下来就非常简单了。

最初的回答

const quotes = []; // Array of quote _ids
const stories = []; // Array of story _ids
const news = []; // Array of news _ids

const apiObject = {
  quotes: quotes,
  stories: stories,
  news: news
};

// Save the API Object to the database
apiObject.save()
.then((savedObject) => {
  // Here is your saved object. Database should be accurate.
});

这种方法的好处在于“数据填充”。当你从数据库中查找API文档时,可以填充每个字段并返回整个对象。将数据填充到对象中,可以使得操作更加方便。
API.find({}).populate('quotes stories news')
.then((apiObjects) => {
// Populated documents
});

此外,如果你想/需要更新一份报价,通过它自己的模型调用Quote.update()更新报价要比尝试通过API模型更新要简单得多。将修改限制在报价模型内可轻松更新报价,避免了复杂的API调用。

1
这似乎有所帮助...感谢您的回答...现在是测试路由的时候了。 - Basit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接