fug*_*ugu 6 javascript mongoose mongodb node.js
我有一个猫鼬的模式:
models / profile.js
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var profileSchema = new mongoose.Schema({
username: String,
complete: { type: Boolean, default: false },
email: { type: String, default: "" },
city: { type: String, default: "" }
}, { discriminatorKey: 'accountType' });
profileSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model('Profile', profileSchema);
Run Code Online (Sandbox Code Playgroud)
与之相关的两个鉴别符:
型号/finder.js
var Profile = require('./profile');
var Finder = Profile.discriminator('finder', new mongoose.Schema({
position: { type: String, default: "" },
skills: Array
}));
module.exports = mongoose.model("Finder");
Run Code Online (Sandbox Code Playgroud)
models / helper.js
var Profile = require('./profile');
var Helper = Profile.discriminator('helper', new mongoose.Schema({
jobTitle: { type: String, default: "" },
lastPosition: { type: String, default: "" }
}));
module.exports = mongoose.model("Helper");
Run Code Online (Sandbox Code Playgroud)
我在一个快速的框架中使用它,并且在一页上(如下所示)中,我要遍历键/值对Profile以构建表。
我想保留在Schema中指定的顺序,以使页面之间的表顺序保持一致。
是否可以在模式创建时定义排序顺序?
这是我profile.ejs制作表格的文件:
<table class="table profile-display">
<tbody>
<% for(var key in profile.toObject({versionKey: false})) { %>
<% if (key != '_id') { %>
<% if (profile[key].length === 0){ %>
<tr class="incomplete-warning table-rows">
<% } else { %>
<tr class="table-rows">
<% } %>
<td class="key-text"><%=key.toUpperCase()%>:</td>
<td><%=profile[key]%></td>
</tr>
<% } %>
<% } %>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
请让我知道是否可以提供更多信息
每个浏览器处理对象排序的方式都不同。我建议返回一个 JSON 对象,该对象映射具有排序值等的架构并迭代您的模板profile.ejs。然后你可以根据需要映射 mongoose 输出键的值。
{
1 : 'username',
2 : 'type',
3 : 'complete',
4 : 'email',
5 : 'city',
6 : 'position',
7 : 'skills'
}
Run Code Online (Sandbox Code Playgroud)
或一个数组
[
'username',
'type',
'complete',
'email',
'city',
'position',
'skills'
]
Run Code Online (Sandbox Code Playgroud)
然后,您可以从对象的值或数组映射您的模式。我喜欢在这种情况下使用数组,因为仅使用索引键就可以更轻松地进行迭代。这取决于您的用途和目的。
希望有帮助。
更新:为了最大限度地减少两次硬编码模式,您可以创建一个与模式值排序的对象。
代码示例。
// Construct your object
var objSchema = {
obj1 : {
schema : { type: String },
order : 2
},
obj2 : {
schema : { type: String },
order : 3
},
obj3 : {
schema : { type: String },
order : 1
}
}
// Function to construct your schema object and sort array
function mapSortSchema(objs) {
var result = {
schema : {},
order : []
}
for (var obj in objs) {
result.schema[obj] = objs[obj].schema;
result.order.push({
'key': obj,
'order': objs[obj].order
});
}
result.order.sort(function(a, b) {
return a.order - b.order;
});
return result;
}
Run Code Online (Sandbox Code Playgroud)
现在您有了猫鼬的架构和模板的订单。
var mapSchema = mapSortSchema(objSchema);
// You can now use this with your mongoose schema
var forMongooseSchema = mapSchema.schema;
// result {obj1 : { type: String }, obj2 : { type: String }, obj3 : { type: String }}
// You can now use this to loop through your template
var forTemplateLoop = mapSchema.order;
// result [{key: "obj3", order: 1}, {key: "obj1", order: 2}, {key: "obj2", order: 3}]
Run Code Online (Sandbox Code Playgroud)
还没有在猫鼬中测试过这个,但它会给你基本的想法,你可以根据你的需要改进功能。希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
714 次 |
| 最近记录: |