【问题标题】:select only subdocuments or arrays仅选择子文档或数组
【发布时间】:2012-03-06 13:24:26
【问题描述】:
{
    "_id":{
        "oid":"4f33bf69873dbc73a7d21dc3"
    },
    "country":"IND",
    "states":[{
            "name":"orissa",
            "direction":"east",
            "population":41947358,
            "districts":[{
                    "name":"puri",
                    "headquarter":"puri",
                    "population":1498604
                },
                {
                    "name":"khordha",
                    "headquarter":"bhubaneswar",
                    "population":1874405
                }
            ]
        },
        {
            "name":"andhra pradesh",
            "direction":"south",
            "population":84665533,
            "districts":[{
                    "name":"rangareddi",
                    "headquarter":"hyderabad",
                    "population":3506670
                },
                {
                    "name":"vishakhapatnam",
                    "headquarter":"vishakhapatnam",
                    "population":3789823
                }
            ]
        }
    ]
}

在上述集合(即国家/地区)中,我只有一个文档,我想获取有关特定州的详细信息(比如说“country.states.name”:“orissa”),但我希望我的结果在这里在而不是整个文档下。Mogo中有没有办法...

     {
    "name": "orissa",
    "direction": "east",
    "population": 41947358,
    "districts": [
        {
            "name": "puri",
            "headquarter": "puri",
            "population": 1498604
        },
        {
            "name": "khordha",
            "headquarter": "bhubaneswar",
            "population": 1874405
        }
    ]
   }

谢谢

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    如果您不想使用aggregate,您可以在应用层使用下划线(默认包含)非常轻松地做到这一点:

    var country = Groops.findOne({"property":value);
    var state _.where(country, {"state":statename});
    

    这将为您提供与statename 匹配的整个状态记录。很方便。

    【讨论】:

      【解决方案2】:
      db.countries.find({ "states":  { "$elemMatch": { "name": orissa }}},{"country" : 1, "states.$": 1 })
      

      【讨论】:

        【解决方案3】:

        试过这个:

        db.countries.aggregate(      
            {
                "$project": {
                    "state": "$states",
                    "_id": 0
                }
            },
            {
                "$unwind": "$state"
            },
            {
                "$group": {
                    "_id": "$state.name",
                    "state": {
                        "$first": "$state"
                    }
                }
            },
            {
                "$match": {
                    "_id": "orissa"
                }
            }
        );
        

        得到:

        {
            "result" : [
                    {
                            "_id" : "orissa",
                            "state" : {
                                    "name" : "orissa",
                                    "direction" : "east",
                                    "population" : 41947358,
                                    "districts" : [
                                            {
                                                    "name" : "puri",
                                                    "headquarter" : "puri",
                                                    "population" : 1498604
                                            },
                                            {
                                                    "name" : "khordha",
                                                    "headquarter" : "bhubaneswar",
                                                    "population" : 1874405
                                            }
                                    ]
                            }
                    }
            ],
            "ok" : 1
        

        【讨论】:

          【解决方案4】:

          你现在不能这样做,但你可以在aggregation framework 中使用 $unwind。你现在可以试试 2.1 的实验分支,稳定版会在 2.2 出来,大概几个月后。

          【讨论】:

          【解决方案5】:

          mongodb 中的任何查询总是返回根文档。

          如果您知道嵌套数组中的状态序数,则只有一种方法可以通过 $slice 加载一个带有父文档的子文档:

          // skip ordinalNumberOfState -1, limit 1
          db.countries.find({_id: 1}, {states:{$slice: [ordinalNumber -1 , 1]}}) 
          

          $slice 以默认顺序工作(因为文档被插入到嵌套数组中)。 此外,如果您不需要某个国家/地区的字段,则可以在结果中仅包含 _id 和 states:

          db.countries.find({_id: 1}, {states:{$slice: [ordinalNumber -1 , 1]}, _id: 1}) 
          

          然后结果文档将如下所示:

          {
              "_id":{
                  "oid":"4f33bf69873dbc73a7d21dc3"
              },
              "states":[{
                      "name":"orissa",
                      "direction":"east",
                      "population":41947358,
                      "districts":[{
                              "name":"puri",
                              "headquarter":"puri",
                              "population":1498604
                          },
                          {
                              "name":"khordha",
                              "headquarter":"bhubaneswar",
                              "population":1874405
                          }
                      ]
                  }]
          }
          

          【讨论】:

          • 那我们可以在 Mongo 中写一个程序,先获取状态的序数,然后再使用 $slice 吗?
          • @John: 不,但是您可以在将国家/地区插入数据库时​​将id 添加到等于州序数的每个州。然后使用这个 id 来加载特定的状态。
          猜你喜欢
          • 2017-10-17
          • 2017-11-18
          • 2021-05-10
          • 2019-01-16
          • 1970-01-01
          • 1970-01-01
          • 2013-09-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多