繁体   English   中英

在猫鼬表达中选择一些字段作为数组

[英]Select few fields as array in mongoose express

如果在我的模式数据集中,我有...

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;
        });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM