在mongoose express中选择几个字段作为数组
Select few fields as array in mongoose express
如果在我的modle Schema数据集中我有。。。
var visitSchema = new mongoose.Schema({
title : { type: String},
client : { type: String},
agenda : { type: String},
agm : { type: String },
anchor :{ type: String }
)}
所以在这里,我想要一些其他的服务,将标题、客户端、议程分离到一个数组中,并将agm锚定到另一个数组以检索
所以我试过这种方式。。。
function getExecsById(id){
var deferred = Q.defer();
console.log("im in getExecsById")
var one = ('agm anchor');
//var two =('title client agend');
model
.find({ _id: id })
.select(one)
.exec(function (err, item) {
item = item.map(function(doc) { return doc.one; });
if(err) {
console.log(err);
deferred.reject(err);
}
else
deferred.resolve(item);
});
return deferred.promise;
}
注意:不要更改模式。。。只有在服务中,才能将数据字段分离到两个数组中,并获取数据。。。。。
帮助提前感谢
我假设Lodash可用。
function getExecsById(id){
var partition = ['agm', 'anchor'];
return model.findOne({ _id: id })
.then(function(item) {
var p1 = [],
p2 = [];
return _.each(item, function(v, k) {
if (partition.indexOf(k) !== -1) {
p1.push(v);
}
else {
p2.push(v);
}
});
})
.catch(function(error) {
return error;
});
}
相关文章:
- jQuery Wan Spinner插件的多个字段
- Angular ng repeat order将多个字段作为一个字段
- Dynamics CRM 2016:自动完成多个字段
- 更新嵌套对象的多个字段
- 使用单个文本框向多个字段添加严格搜索
- 当出现多个字段时,jquery时间条目问题
- 使用数组表示法对多个字段执行jQuery日期选择器
- 如何使用2个字段名称作为筛选器
- 如何使用jquery将两个字段组合为下拉菜单中的选项
- 如何防止某些形式's个字段无法提交
- Angularjs 通过选择不同选择框中的两个字段来填充选择框
- 如何在一个表单标记中验证多个字段集
- 使用Join,要求两个字段中的一个字段为非空
- 在原始对象的基础上创建新对象的最简单方法,但没有几个字段
- jQuery在几个字段中倒计时
- 在mongoose express中选择几个字段作为数组
- Rivets.js属性依赖于几个字段
- Rails:如何编写一个测试,以确保几个字段在关闭时隐藏,但在打开时显示,并且具有正确的值
- 猫鼬自定义验证几个字段在更新
- 使用Javascript显示和隐藏几个字段