Node.js 为平均应用生成猫鼬DTO
我不熟悉mongoose和node,我正在使用MEAN stack(Mongo ExpressJS AngularJS node)构建一个应用程序 过去我使用asp.net WebAPI构建了很多API,但我找不到任何关于使用DTO或视图模型的文档,以减少在服务器和前端之间来回传输JSON的量 我的申请是关于一个用户在线填写的调查。然后,每个答案由用户生成分数 我的模型:Node.js 为平均应用生成猫鼬DTO,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我不熟悉mongoose和node,我正在使用MEAN stack(Mongo ExpressJS AngularJS node)构建一个应用程序 过去我使用asp.net WebAPI构建了很多API,但我找不到任何关于使用DTO或视图模型的文档,以减少在服务器和前端之间来回传输JSON的量 我的申请是关于一个用户在线填写的调查。然后,每个答案由用户生成分数 我的模型: var UserSchema = new Schema({ email: {type: String, trim:
var UserSchema = new Schema({
email: {type: String, trim: true,default: '', match: [/.+\@.+\..+/,'']},
status: {type: String},
token:{type: String, default: crypto.randomBytes(64).toString('hex')},
score: {
managementExperience: {type: Number},
managementSkills: {type: Number},
relevantKnowledge: {type: Number},
commitment: {type: Number},
acceptanceOfChange: {type: Number},
age: {type: Number},
totalScore: {type: Number}
},
answers: [
{
optionId: {type: Schema.Types.ObjectId}
}
]
});
var SurveySchema = new Schema({
client_id:{type: Schema.Types.ObjectId, ref: 'Client' },
creationDate:{type: Date,default: Date.now},
title: {type: String, trim: true},
surveyVersion: { type: Schema.Types.ObjectId, ref: 'SurveyVersion' },
users:[UserSchema]
});
调查屏幕本身可以工作,但在生成结果仪表板时,我希望发送DTO,而不是整个调查模式,如以下模型:
var SurveySchemaLight = new Schema({
client_id:{type: Schema.Types.ObjectId, ref: 'Client' },
creationDate:{type: Date,default: Date.now},
title: {type: String, trim: true},
users:[{
email: {type: String, trim: true,default: '', match: [/.+\@.+\..+/,'']},
status: {type: String}
}]
});
在.Net世界中,我希望这个模型有一个构造函数,它将SurveySchema的一个实例作为参数,但我找不到一种方法使它工作
我还尝试将两个架构链接到mongodb中的同一个集合:
mongoose.model('Survey', SurveySchema);
mongoose.model('SurveyLight', SurveySchemaLight, 'surveys');
但当我在SurveyLight模式上运行以下查询时,仍然返回了Survey中的所有字段:
SurveyLight.find({'client_id': req.params.clientID}).exec(function(err, surveyList){
res.json(surveyList);
});
在堆栈中使用DTO/视图模型机制的最佳实践是什么
感谢JavaScript世界,您应该尝试从函数编程的角度来解决这个问题。如果您有一些数据,则希望返回该数据的子集。您不需要单独的模型定义,只需要一个映射/过滤函数 e、 g 通过使用流行的函数库(如.对于“mongoose”和MongoDB查询),您可以做得更好。一般来说,您只需“投影”输出中所需的字段,而忽略不需要的其他字段 作为一个独立的示例:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
var childSchema = new Schema({
"longName": String,
"email": String,
"status": String
})
var parentSchema = new Schema({
"name": String,
"longDescription": String,
"children": [childSchema]
})
mongoose.connect('mongodb://localhost/test');
var Parent = mongoose.model( 'Parent', parentSchema );
async.waterfall(
[
// remove any samples
function(callback) {
Parent.remove({},function(err,res) {
callback(err)
});
},
// insert some test data
function(callback) {
Parent.create(
{
"name": "Bill",
"longDescription": "Something we don't want to see",
"children": [
{ "longName": "don't want", "email": "a@example.com", "status": "A" },
{ "longName": "don't want", "email": "b@example.com", "status": "B" }
]
},
function(err,doc) {
console.log( doc );
callback(err,doc);
}
)
},
// Fetch just the fields we want
function(doc,callback) {
Parent.find({},"name children.email children.status", callback);
}
],
// Ouput results
function(err,result) {
if (err) throw err;
console.log(result);
process.exit();
}
);
以下形式的输出:
// The original form of the documents
{ __v: 0,
name: 'Bill',
longDescription: 'Something we don\'t want to see',
_id: 5598b6bad439a31807bfe746,
children:
[ { longName: 'don\'t want',
email: 'a@example.com',
status: 'A',
_id: 5598b6bad439a31807bfe748 },
{ longName: 'don\'t want',
email: 'b@example.com',
status: 'B',
_id: 5598b6bad439a31807bfe747 } ] }
// The output document with just selected fields
[ { _id: 5598b6bad439a31807bfe746,
name: 'Bill',
children:
[ { email: 'a@example.com', status: 'A' },
{ email: 'b@example.com', status: 'B' } ] } ]
如果您想“排除”字段,而不是显式地命名所有字段,请使用-
作为前缀,如中所示
"-longName -children.logDescription"
但是你不能“混合”这两个术语,除了
“-\u id”
谢谢尤里,我更想问的是是否可以使用猫鼬机制来避免这种代码。谢谢Blakes,我不知道这种语法来列出要输出的字段!
"-longName -children.logDescription"