MongoDB查找特定元素并进行排序 [英] MongoDB finding specific elements and sorting

查看:96
本文介绍了MongoDB查找特定元素并进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我收集的数据:

{ "installation" : 200278 , 
      "date" : [ 
           { "date" : "2014-12-28" , 
                "values" : [ 
                      { "time" : "2014-12-28 00:15:26" , "value" : 26}, 
                      { "time" : "2014-12-28 00:30:26" , "value" : 15} , 
                      { "time" : "2014-12-28 00:45:26" , "value" : 7} , 
                      { "time" : "2014-12-28 01:00:26" , "value" : 32}
                ]
           }, 
           { "date" : "2014-12-29" , 
                "values" : [ 
                      { "time" : "2014-12-29 00:15:26" , "value" : 26}, 
                      { "time" : "2014-12-29 00:30:26" , "value" : 15} , 
                      { "time" : "2014-12-29 00:45:26" , "value" : 7} , 
                      { "time" : "2014-12-29 01:00:26" , "value" : 32}
                ]
           }
     ]
}, 
{ "installation" : 200312
    ...

我尝试查询以下内容:

db.measure.find({"installation" : 200278 , "date.date" : "2014-12-28"}, 
                {"date.date" : 1 , "_id" : 0})
                .sort({"date.date" : 1})

首先,我不明白为什么"date.date"不仅返回带有"2014-12-28"的元素. 另外,也许我排序有误,但不管我在1上的符号为何,它仍然会返回相同的顺序.

First of all I do not understand why "date.date" does not only return the element with "2014-12-28". In addition maybe I get sorting wrong but not matter my sign on the 1 it still returns the same order.

结果:

{ "date" : [ { "date" : "2014-12-28" }, { "date" : "2014-12-29" } ] }

说明

我希望查询返回的只是元素:

What I want my query to return is only the element:

{"date":"2014-12-28"}

{ "date" : "2014-12-28" }

排序问题是我对Mongo API的黑客攻击的延伸.并且想知道该查询如何工作.

The sorting problem is an extension of my hacking on the Mongo api. And a wondering of how this querying works.

推荐答案

您需要汇总结果.

当执行与find()链接的sort()时,排序适用于由find()查询过滤的根文档,而不是数组子文档.

When you perform a sort() chained with a find(), the sort applies to the root documents that were filtered by the find() query, and not the array sub documents.

为了对数组元素进行排序,您需要使用聚合管道,如下所示:

In order to sort array elements, you need to use the aggregation pipeline as below:

db.measure.aggregate([
{$match:{"installation" : 200278,"date.date" : "2014-12-28"}},
{$unwind:"$date"},
{$sort:{"date.date":-1}},
{$group:{"_id":"$id","installation":{$first:"$installation"},"date":{$push:"$date"}}},
{$project:{"_id" : 0,"installation":1,"date.date":1}}
])

首先,我不明白为什么"date.date"不只返回"2014-12-28"元素.

要仅获取与查询匹配的第一个数组元素,您需要使用 $ (位置运算符).

To get only the first array element that matches the query, you need to use the $(positional operator).

db.measure.find({"installation" : 200278 , 
                 "date.date" : "2014-12-28"}, 
                 {"date.$" : 1,"_id":0})

如果希望保持date数组始终按date排序,则可以确保在使用$each$sort更新运算符对数组进行更新时.

If you wish to keep the date array always sorted by date, you could ensure that while making updates to the array, by making use of the $each and $sort update operators.

db.measures.update({"installation":200278},
            {$push:{
                    date:{
                          $each:[{date object},
                                 {date object},...],
                          $sort:{"date":-1}, // sort the date array by `date` 
                                             // field during updates
                         }
            }}) 

因此,查找查询将始终为每次安装返回日期文档的排序数组.

So a find query would always return a sorted array of date documents for each installation.

这篇关于MongoDB查找特定元素并进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆