我即将为我刚刚构建的表单创建一个巨大的模式...据说我的模式顺序是否必须模仿表单顺序,或者它是否可以按照我放入的任何顺序包含所有输入? 下面的例子。 可以这样吗?
// link to mongoose
var mongoose = require('mongoose');
// define the article schema
var mapSchema = new mongoose.Schema({
created: {
type: Date,
default: Date.now
},
dd1: {
type: String,
default: ''
},
dd2: {
type: String,
default: ''
},
com1: {
type: String,
default: ''
},
com2: {
type: String,
default: ''
}
});
// make it public
module.exports = mongoose.model('Map', mapSchema);
还是一定要这样?
// link to mongoose
var mongoose = require('mongoose');
// define the article schema
var mapSchema = new mongoose.Schema({
created: {
type: Date,
default: Date.now
},
dd1: {
type: String,
default: ''
},
com1: {
type: String,
default: ''
},
dd2: {
type: String,
default: ''
},
com2: {
type: String,
default: ''
}
});
// make it public
module.exports = mongoose.model('Map', mapSchema);
最佳答案
does my schema order have to mimic the form order, or can it just have all the inputs in any order I put them in?
mongoose.Schema
接受一个 JavaScript 对象作为它的参数。所以你的问题归结为:
JavaScript 对象是否知道其键的定义顺序?
答案是:不,JavaScript 对象中不维护键顺序。 JS 规范明确指出对象是无序的键/值集合。 ( compare )
因此,mongoose.Schema
不能依赖键顺序,即使它绑定(bind)到,这意味着您可以自由地以任何您喜欢的方式对键进行排序。
我们也可以从另一端来解决这个问题:
是否有可能是像表单字段顺序这样的前端更改迫使我重写数据库后端代码?
答案是:不,这不太可能。我们甚至可以在不查看任何规范的情况下打消这个想法,因为它没有任何意义。
关于javascript - 模式顺序在 Node.JS 中重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36727445/