dongpu1881 2015-06-16 12:25
浏览 83
已采纳

获取mongodb数组中的匹配元素

I want to use aggregation to get this array only with those tickets, which have start field after 2015-06-16. Can someone help me with the pipeline?

{
    "name" : "array",
    "tickets" : [ 
        {
            "id" : 1,
            "sort" : true,
            "start" : ISODate("2015-06-15T22:00:00.000Z")
        },
        {
            "id" : 2,
            "sort" : true,
            "start" : ISODate("2015-06-16T22:00:00.000Z")
        },
        {
            "id" : 3,
            "sort" : true,
            "start" : ISODate("2015-06-17T22:00:00.000Z")
        }
    ]
}
  • 写回答

2条回答 默认 最新

  • dongpi2503 2015-06-16 12:46
    关注

    It's true that the "standard projection" operations available to MongoDB methods such as .find() will only return at most a "single matching element" from the array to that is queried by either the positional $ operator form in the "query" portion or the $elemMatch in the "projection" portion.

    In order to do this sort of "ranged" operation, you need the aggregation framework which has greater "manipulation" and "filtering" capabilities on arrays:

     collection.aggregate(
         array( 
             # First match the "document" to reduce the pipeline
             array(
                 '$match' => array(
                     array( 
                        'tickets.start' => array(
                            '$gte' => new MongoDate(strtotime('2015-06-16 00:00:00'))
                        )
                    )
                 )
             ),
    
             # Then unwind the array
             array( '$unwind' => '$tickets' ),
    
             # Match again on the "unwound" elements to filter
             array(
                 '$match' => array(
                     array( 
                        'tickets.start' => array(
                            '$gte' => new MongoDate(strtotime('2015-06-16 00:00:00'))
                        )
                    )
                 )
             ),
    
             # Group back to original structure per document
             array(
                 '$group' => array( 
                      '_id' => '$_id',
                      'name' => array( '$first' => '$name' ),
                      'tickets' => array(
                          '$push' => '$tickets'
                      )
                 )
             )           
         )
     ) 
    

    Or you can possibly use the $redact operator to simplify with MongoDB 2.6 or greater which basically uses the $cond operator syntax as it's input:

     collection.aggregate(
         array( 
             # First match the "document" to reduce the pipeline
             array(
                 '$match' => array(
                     array( 
                        'tickets.start' => array(
                            '$gte' => new MongoDate(strtotime('2015-06-16 00:00:00'))
                        )
                    )
                 )
             ),
    
             # Redact entries from the array
             array(
                 '$redact' => array(
                     'if' => array(
                         '$gte' => array(
                             array( '$ifNull' => array(
                                 '$start',
                                 new MongoDate(strtotime('2015-06-16 00:00:00'))
                             )),
                             new MongoDate(strtotime('2015-06-16 00:00:00:00'))
                         )
                     ),
                     'then' => '$$DESCEND',
                     'else' => '$$PRUNE'
                 )
             )
         )
     ) 
    

    So both examples do the "same thing" in "filtering" the elements from the array that "do not" match the conditions specified and return "more than one" element, which is something basic projection cannot do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?