如何匹配MongoDB中的聚合($ graphLookup)元素?

时间:2018-02-24 15:50:16

标签: mongodb

让我们说:

{ "_id": 1, "name": "Dev", final: false }
{ "_id": 2, "name": "Eliot", "reportsTo" : "Dev", final: false }
{ "_id": 3, "name": "Ron", "reportsTo" : "Eliot", final: false }
{ "_id": 4, "name": "Andrew", "reportsTo" : "Eliot", final: false }
{ "_id": 5, "name": "Asya", "reportsTo" : "Ron", final: true }
{ "_id": 6, "name": "Dan", "reportsTo" : "Andrew", final: true }

申请后:

db.employees.aggregate([
    {$match: { final: true }},
    {$graphLookup: {
        ...
        as: "reportingHierarchy"
    }}
])

如何通过匹配层次结构对象reportingHierarchy.[].name中显示的属性来过滤结果。例如,同时包含AndrewEliot

{ "_id": 6, "name": "Dan", "reportsTo" : "Andrew", final: true,
    "reportingHierarchy": [
        { "_id": 1, "name": "Dev", final: false },
        { "_id": 2, "name": "Eliot", "reportsTo" : "Dev", final: false },
        { "_id": 4, "name": "Andrew", "reportsTo" : "Eliot", final: false }
    ]
}

2 个答案:

答案 0 :(得分:1)

function renderGraph(xAxisSignal, yAxisSignal,fromDate,toDate,deviceID) { $.ajax({ type: "GET", url: "/Certificate/GetSelectedSignalData", data: JSON.stringify({ 'xAxisSignal': xAxisSignal, 'yAxisSignal': yAxisSignal, ...}), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { drawGraph(data); }, failure: function (data) { //alert(data.responseText); }, error: function (data) { //alert(data.responseText); } }); } 是一个常规的对象数组,因此您可以将下一个reportingHierarchy阶段添加到您的聚合

$match

答案 1 :(得分:0)

我发现我的案例的正确解决方案是:

{$match: 
  {$and: [
    {"reportingHierarchy.name": {$regex: 'And'}}, 
    {"reportingHierarchy.name": {$regex: 'El'}}
  ]}
}