2014-10-10 135 views
1

以下是我設計的「Famous Recipes」數據庫的概念結構。

該結構與$ redact聚合章節中的MongoDB示例類似。

最大的不同在於我希望對部分和子部分進行命名訪問,以及存儲在「標籤」數組內的對象中的附加信息,而不是每個MongodB示例的簡單字符串。

問題是我無法制作$ redact條件,因此它可以正確檢查tags.name屬性。我在條件內使用$ elemMatch失敗,並且點符號也引發錯誤。

有什麼建議嗎?

{ 
    _id: 1, 
    title: "Secret Recipes of Fast Food Chains", 
    tags: [ 
    { name: "Colonel Sanders", access: ["read"] }, 
    { name: "Ronald McDonald", access: ["read"] } 
    ], 
    year: 2014, 
    subsections: [ 
     { 
     subtitle: "Colonel Sander's Famous Recipe", 
     tags: [ 
     { name: "Colonel Sanders", access: ["update", "delete"] }, 
     { name: "Ronald McDonald"} 
     ], 
     ingredients: ["salt", "pepper", "paprika", "garlic powder"] 
     ] 
    { 
     subtitle: "Ronald McDonald's McNugget Sauce", 
     tags: [ 
     { name: "Colonel Sanders" }, 
     { name: "Ronald McDonald", access: ["update", "delete"] } 
     ], 
     ingredients: ["mustard", "ketchup", "salt", "vinegar"] 
    } 
} 

我不工作的查詢會讀這樣的事:

var access = "Colonel Sanders" 
db.recipes.aggregate(
    [ 
    { $match: { "year": 2014 } }, 
    { $redact: 
    { 
     $cond: { 
       if: { $eq: [ tags: { $elemMatch : { "name" } }, access ] }, 
       then: "$$DESCEND", 
       else: "$$PRUNE" 
       } 
     } 
    } 
    ] 
) 

回答

0

老問題,但我同樣的問題的痛苦。

首先,你的榜樣固定:

db.recipes.insert({ 
    _id: 1, 
    title: "Secret Recipes of Fast Food Chains", 
    tags: [{ 
    name: "Colonel Sanders", 
    access: ["read"] 
    }, { 
    name: "Ronald McDonald", 
    access: ["read"] 
    }], 
    year: 2014, 
    subsections: [{ 
    subtitle: "Colonel Sander's Famous Recipe", 
    tags: [{ 
     name: "Colonel Sanders", 
     access: ["update", "delete"] 
    }, { 
     name: "Ronald McDonald" 
    }], 
    ingredients: ["salt", "pepper", "paprika", "garlic powder"] 
    }, { 
    subtitle: "Ronald McDonald's McNugget Sauce", 
    tags: [{ 
     name: "Colonel Sanders" 
    }, { 
     name: "Ronald McDonald", 
     access: ["update", "delete"] 
    }], 
    ingredients: ["mustard", "ketchup", "salt", "vinegar"] 
    }] 
}) 

之後,我寫了下面的查詢:

var access = "Colonel Sanders" 
db.recipes.aggregate(
    [{ 
    $match: { 
     "year": 2014 
    } 
    }, { 
    $redact: { 
     $cond: { 
     if : { 
      $or: [{$eq: ["$name", access]}, {$not: "$name"}] 
     }, 
     then: "$$DESCEND", 
     else :"$$PRUNE" 
     } 
    } 
    }] 
) 

給一試。你可以在這個問題上找到更多的信息:3985214

相關問題