【问题标题】:Meteor.js + MongoDB :: Retrieve values from deeply nested arraysMeteor.js + MongoDB :: 从深度嵌套的数组中检索值
【发布时间】:2015-09-16 23:33:20
【问题描述】:

我很难得到这个结果:

["Chadstone", "South Yarra"]

当给定这两个值时:

name: 'Soda Rock'
city: 'Melbourne'

来自本文档:

{
    "_id" : "axHqB4NjXbWwphik3",
    "name" : "Soda Rock",
    "storeLocation" : [
        {
            "state" : "Victoria",
            "outlet" : [
                {
                    "city" : "Melbourne",
                    "name" : ["Chadstone", "South Yarra"]
                },
                {
                    "city" : "Geelong",
                    "name" : ["Myer", "Market Square"]
                }
            ]
        },
        {
            "state" : "New South Wales",
            "outlet" : [
                {
                    "city" : "Sydney",
                    "name" : ["Westfield", "Broadway Shopping Centre"]
                }
            ]
        }
    ]
}



我尝试了几种方法,但都没有输出我想要的结果。

方法#1:

Stores.find(
    {
        'name': 'Soda Rock', 
        'storeLocation.outlet.city': 'Melbourne'
    }, 
    {
        _id: 0, 
        'storeLocation.outlet.name.$': 1
    }
);

结果:

{
    "storeLocation" : [
        {
            "state" : "Victoria",
            "outlet" : [
                {
                    "city" : "Melbourne",
                    "name" : ["Chadstone", "South Yarra"]
                },
                {
                    "city" : "Geelong",
                    "name" : ["Myer", "Market Square"]
                }
            ]
        }
    ]
}

方法#2:

Stores.find(
    {
        'name': 'Soda Rock', 
        'storeLocation.outlet.city': {
            'Melbourne'
        }
    }, 
    {
        _id: 0, 
        'storeLocation.outlet.name': 1
    }
);

结果:

{
    "storeLocation" : [
        {
            "outlet" : [
                {
                    "name" : ["Chadstone", "South Yarra"]
                },
                {
                    "name" : ["Myer", "Market Square"]
                }
            ]
        },
        {
            "outlet" : [
                {
                    "name" : ["Westfield", "Broadway Shopping Centre"]
                }
            ]
        }
    ]
}

我也尝试过使用meteorhacks:aggregate,但由于我没有找到对新手友好的文档,所以无法使用。

非常感谢您的解决方案和友好的解释!

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    在这里使用聚合似乎有点过头了。这是一个提取出口名称的示例函数:

    var findOutletName = function(name, city) {
      var doc = Stores.findOne({name: name});
      var outletName = null;
      _.each(doc.storeLocation, function(location) {
        _.each(location.outlet, function(outlet) {
          if (outlet.city === city)
            outletName = outlet.name;
        });
      });
      return outletName;
    };
    

    你可以这样使用:

    findOutletName('Soda Rock', 'Melbourne');
    

    【讨论】:

    • 与我尝试过滤数据相比,这是一个非常紧凑的解决方案。谢谢!
    【解决方案2】:

    可以肯定的是,标准 Meteor 无法做到这一点。聚合包可能会有所帮助,但我也没有使用它。我会使用方法 1 来检索文档,然后过滤文档以使用标准 javascript 找到您想要的信息(或类似下划线的东西可能会帮助您。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2016-04-18
      • 1970-01-01
      • 2019-04-02
      相关资源
      最近更新 更多