【问题标题】:Return values of subdocument in Meteor MongoDB collectionMeteor MongoDB 集合中子文档的返回值
【发布时间】:2015-05-06 17:35:26
【问题描述】:

拥有以下 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"
        }
    ]
}

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

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}
            })
        }
    }

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

【问题讨论】:

  • 不清楚你在问什么。什么不工作?另外,item.states 是一个数组,所以item.states.state_name 显然不能工作。

标签: mongodb meteor meteor-autoform chained-select


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多