[英]Which schema type should I use for an attribute that is an Array of Objects in Mongoose?
I've been searching through questions on here/Google, but haven't found a clear answer. 我一直在这里/ Google上搜索问题,但没有找到明确的答案。 I feel like this should be straightforward.
我觉得这应该很简单。
I am making my personal portfolio site with MEAN stack, have all the front end done, and want to load all my site's content info from the backend, instead of doing it from my Angular Controller. 我正在使用MEAN堆栈制作个人投资组合网站,完成所有前端,并希望从后端加载我网站的所有内容信息,而不是从Angular Controller中加载它。 One example of content to be loaded is different portfolio pieces into my html.
要加载的内容的一个示例是将不同的作品集片段放入html。 Right now, those portfolio pieces are in my Angular controller, are being defined there, and are an array of objects, with 'name', 'language', 'reference', and 'image reference' as my attributes for each project.
现在,这些作品集都在我的Angular控制器中,在那里定义,并且是一个对象数组,其中“名称”,“语言”,“参考”和“图像参考”是我对每个项目的属性。
What I would like to do instead is make a content model in my backend, which has all my site's content in it, which each piece of content being an attribute of the 'Content' model, create routes for it, and link it via Angular services. 我想做的是在后端创建一个内容模型,该模型中包含网站的所有内容,其中每个内容都是“内容”模型的一个属性,为其创建路由,并通过Angular进行链接服务。
I'm not worried about the practicality of using Node/Mongoose here because I'm doing it so I can demonstrate my knowledge of how to use services/node backend, and connecting the two. 我不担心在这里使用Node / Mongoose的实用性,因为我正在这样做,所以我可以展示我如何使用服务/节点后端以及将两者连接的知识。
Here is what the portfolio array looks like in my controller (I want to store this in the backend instead) - 这是我的控制器中投资组合数组的外观(我想将其存储在后端中)-
vm.projects = [
{
name: 'Tic-Tac-Toe',
language: 'ANGULAR.JS',
reference: 'angular',
image: '/assets/images/tic-tac-toe.png'
},
{
name: 'Escrow',
language: 'RUBY ON RAILS',
reference: 'ruby',
image: '/assets/images/escrow.png'
},
{
name: 'Feed.Me',
language: 'RUBY ON RAILS',
reference: 'ruby',
image: '/assets/images/feed-me.png'
},
{
name: 'Gone-In',
language: 'NODE.JS',
reference: 'node',
image: '/assets/images/gone-in.png'
}
];
Here is what my Mongoose model currently looks like. 这是我的猫鼬模型当前的样子。 I'm having trouble defining the schema.
我在定义架构时遇到了麻烦。 These attributes are all the different pieces of content I want to load into my website.
这些属性是我要加载到网站中的所有不同内容。 The attribute that corresponds to the array above is 'projects'.
与上面的数组相对应的属性是“项目”。 This attribute, along with most others, needs to be an array of objects.
此属性以及大多数其他属性必须是对象数组。 I'm not seeing a clear attribute type on Mongoose's official documentation for arrays of objects.
我在Mongoose的官方文档中没有看到用于对象数组的明确属性类型。 I don't feel like [Schema.Types.Mixed] is really the correct answer.
我觉得[Schema.Types.Mixed]确实不是正确的答案。 What is?
什么是?
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var contentSchema = new Schema({
aboutParagraphs: [String],
socialMediaProfiles: [Schema.Types.Mixed],
skills: [Schema.Types.Mixed],
languages: [Schema.Types.Mixed],
projects: [Schema.Types.Mixed],
schools: [Schema.Types.Mixed],
companies: [Schema.Types.Mixed]
})
You define the structure of embedded sub-documents using their own schema. 您可以使用其自己的架构定义嵌入式子文档的结构。
So in this case you can create: 因此,在这种情况下,您可以创建:
var projectSchema = new Schema({
name: String,
language: String,
reference: String,
image: String
});
And then use the schema as the type
when you define your projects
array: 然后,在定义
projects
数组时将架构用作type
:
var contentSchema = new Schema({
aboutParagraphs: [String],
...
projects: [projectSchema],
...
});
Note that you can also declare the sub-document schema implicitly, right in the parent schema: 请注意,您还可以在父模式中隐式声明子文档模式:
var contentSchema = new Schema({
aboutParagraphs: [String],
...
projects: [{
name: String,
language: String,
reference: String,
image: String
}],
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.