javascript - 模式顺序在 Node.JS 中重要吗?

标签 javascript node.js mongodb schema mean-stack

我即将为我刚刚构建的表单创建一个巨大的模式...据说我的模式顺序是否必须模仿表单顺序,或者它是否可以按照我放入的任何顺序包含所有输入? 下面的例子。 可以这样吗?

// 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/

相关文章:

python - MongoDB M0集群,python多线程

javascript - 在对象内部定义的函数中声明的 this 关键字究竟如何工作?

javascript - 我将如何为一个元素添加一个顶部偏移量,当它到达窗口顶部时,该元素会粘住?

javascript - 我的正则表达式在 Node.js 中不起作用?

python - pymongo 更新不起作用

json - 是否有提供 JSON 记录的关系数据库?

javascript - 代码在 HTML 页面中有效,但在 Wordpress 帖子中无效

javascript - Google SignIn SDK 因抛出错误而失败,发生不可恢复的登录失败 - 捕获错误 : React Native

javascript - Azure:Web 应用程序 - 列出已部署的 Nodejs 应用程序中的应用程序设置

javascript - 如何从 Mongodb 中删除数组元素