在对象mongodb数组中的字段上查找管道

时间:2018-01-12 15:38:22

标签: javascript node.js mongodb mongoose lookup

// MongoDb User Object:

const new_schema = new Schema({
    name: {
        first: {type: String},
        last: {type: String},
    },
    currentPrivateLocation: {type: [String], default: []} 
});



// Aggregation:

let pipeline = [{
    $match: query
}];

pipeline.push({
  $sort: sortObj
});

pipeline.push({
    $group: {
        _id: null,
        count: {$sum: 1},
        result: {$push: '$$ROOT'},
    }
});

pipeline.push({
    $project: {
        _id: 0,
        count: 1,
        result: {
            $slice: ['$result', Number(limit * (page - 1)), limit]
        }
    }
});

在MongoDB中,我聚合一个集合以获取这种格式的数据:

{ 
    count: 2,
    result: 
    [ 
        { _id: 57e55d55d9a7041100ac5e26,
          subjectIds: [Object],
          role: [Object]
        },
        { _id: 58e5d545g9a7041500ac4526,
          subjectIds: [Object],
          role: [Object]
        }
    ]
}

我现在想根据$lookup_id对象的'result'来做$unwind。当我尝试var a = [ ["Ilhem", "Ben Salah", 4, 1], ["Ilhem", "Ben Salah", 3, 2], ["Ilhem", "Ben Salah", 3, 3], ["Ilhem", "Ben Salah", 3, 4], ["Sghaier", "Mahmoud", 1, 1], ["Sghaier", "Mohammad", 15, 2], ]; var result = []; while(a.length>0) { var item = a[0], firstname = item[0], lastname = item[1]; var matchedarr = a.filter(function(item){ return (item[0] == firstname && item[1] == lastname); }); for(var j=1;j<=12;j++) { var arr = matchedarr.filter(function(item){ return item[3]==j; }); arr = arr[0] || [item[0],item[1],0,j]; result.push(arr); } a = a.filter(function(item){ return item[0]!==firstname || item[1]!==lastname; }); } console.log(result);结果时,它只是将结果数组转换为仅包含第一个元素的结果对象。

有什么想法吗?

0 个答案:

没有答案