返回Meteor MongoDB集合中子文档的值

时间:2022-04-23 04:17:03

Have the following MongoDB collection:

拥有以下MongoDB集合:

{
    "_id": "u3Yv2bm4cpsssLSHv",
    "allowed": 1,
    "country_name": "Canada",
    "states": [
        {
            "state_name": "Alberta"
        },
        {
            "state_name": "British Columbia"
        },
        {
            "state_name": "Manitoba"
        },
        {
            "state_name": "New Brunswick"
        },
        {
            "state_name": "Newfoundland and Labrador"
        },
        {
            "state_name": "Nova Scotia"
        },
        {
            "state_name": "Ontario"
        },
        {
            "state_name": "Prince Edward Island"
        },
        {
            "state_name": "Quebec"
        },
        {
            "state_name": "Saskatchewan"
        },
        {
            "state_name": "Northwest Territories"
        },
        {
            "state_name": "Nunavut"
        },
        {
            "state_name": "Yukon"
        }
    ]
}

How can I return the values of the "states" in this collection, so I can populate a dropdown? I am trying to do something as follows (does not return anything in the "states" subdocument"):

如何返回此集合中“状态”的值,以便我可以填充下拉列表?我正在尝试执行以下操作(不会在“states”子文档中返回任何内容“):

venue_province: {
    type: String,
    label: "Province/State",
    optional: true,
    max: 200,
    autoform: {
        type: 'chained',
        // Gets a list of province/state names from the locations collection
        options: function() {
            return _.map(bt_col_locations.find().fetch(), function(item) {
                return {value: item.states.state_name, label: item.states.state_name, parent: item.country_name}
            })
        }
    }

Note that I am using Aldeed:Autoforms and jQuery Chained; basically I have a parent dropdown for the user to select the country, and the child dropdown cascades with the appropriate states/provinces.

请注意,我使用的是Aldeed:Autoforms和jQuery Chained;基本上我有一个父下拉列表供用户选择国家,子下拉列表与适当的州/省级联。

1 个解决方案

#1


It seems what you are looking for is probably the Mongo Aggregation framework. (http://docs.mongodb.org/manual/core/aggregation-introduction/) I haven't tested your specific usage here, but with that you should be able to use $unwind to get the states array into a more workable state and then just query that. From there you can also do a few other useful operations, like sorting, to make you list look nice.

看起来你正在寻找的可能是Mongo Aggregation框架。 (http://docs.mongodb.org/manual/core/aggregation-introduction/)我没有在这里测试你的具体用法,但是你应该能够使用$ unwind来使状态数组更加可行状态,然后只是查询。从那里你还可以做一些其他有用的操作,比如排序,让你的列表看起来不错。

#1


It seems what you are looking for is probably the Mongo Aggregation framework. (http://docs.mongodb.org/manual/core/aggregation-introduction/) I haven't tested your specific usage here, but with that you should be able to use $unwind to get the states array into a more workable state and then just query that. From there you can also do a few other useful operations, like sorting, to make you list look nice.

看起来你正在寻找的可能是Mongo Aggregation框架。 (http://docs.mongodb.org/manual/core/aggregation-introduction/)我没有在这里测试你的具体用法,但是你应该能够使用$ unwind来使状态数组更加可行状态,然后只是查询。从那里你还可以做一些其他有用的操作,比如排序,让你的列表看起来不错。