过滤和显示数据

时间:2016-08-04 13:13:38

标签: mongodb meteor

这是我的last questionhere是我采用的解决方案。但我有另一个问题。

这就是我想要显示我的类别的方式,但我在类别中也有子类别。

class TwoVC: UIViewController {
let oneVC = OneVC()

 override viewDidLoad() {
  oneVC.addObserver(self, forKeyPath: "isDragToUpdate", options: [], context: nil)
}

 override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    print("hoh")
    guard keyPath == "isDragToUpdate" else {return}
    print("hah")        
}

 deinit {
    oneVC.removeObserver(self, forKeyPath: "isDragToUpdate")
}
}

这就是我的数据结构

<ul>
    {{#each group in groups}}
      <li class="col-md-2">
        <ul>
          {{#each category in group}}
            <li class="dropdown-header>{{category.category}}</li>
             // <li>*Subcategories</li> this is where i want to display it
          {{/each}}
        </ul>
      </li>
     {{/each}}
    </ul>

你可以看到,一个类别可以有很多子类别。那么,我如何显示类别和子类别,以便我不会获得两次类别

1 个答案:

答案 0 :(得分:0)

我认为你应该在这里使用(子)聚合:

Pure Mongo

db.myData.aggregate([{"$group": {_id: "$category", subcategoriess: { $addToSet: "$subcategory" }}}]);

给你:

{ "_id" : "cat3", "subcategoriess" : [ "subcat3.2", "subcat3" ] }
{ "_id" : "cat2", "subcategoriess" : [ "subcat2" ] }
{ "_id" : "cat1", "subcategoriess" : [ "subcat1.3", "subcat1.2", "subcat1" ]}

然后,您可以迭代结果以获取类别(_id),然后迭代子类别以获取每个类别下的列表。

流星:

var myDataCol = new Mongo.Collection('myData');
var pipeline = [
  {"$group": {_id: "$category", subcategoriess: { $addToSet: "$subcategory" } }}
];
var result = myDataCol.aggregate(pipeline);