0
データをソートすることは可能ですか?グループからの価値。 データ:MongoDBグループとソート文書
> db.test.find({})
{"a" : 1, "d" : 10 }
{"a" : 1, "d" : 1 }
{"a" : 2, "d" : 50 } // changed, it was initially ({"a" : 2, "d" : 20 })
{"a" : 2, "d" : 2 }
{"a" : 3, "d" : 30 }
{"a" : 3, "d" : 4 }
{"a" : 1, "d" : 10 }
結果:
:2、D:50 | a:2:d:2 | a:3、d:30 | a:3、d:4 | a:1、d:10 | a:1、d:10 | :1、d 1は
SQL相当:
create table test(a integer, d integer);
insert into test
select 3, 30 union all
select 3, 4 union all
select 2, 20 union all
select 2, 2 union all
select 1, 10 union all
select 1, 10 union all
select 1, 1;
select a, d, max(d) over (partition by a) as max_in_group
from test
order by 3 desc, 2 desc;
または
select *
from test t
inner join
(
select a, max (d) as max_d
from test
group by a
)X on t.a = x.a
order by max_d desc, d desc
感謝。
ありがとうございます。良いですね! – DraganS
私は気にします。与えられた式はユーザーによってメッセージをグループ化すると仮定し、データ+ページングで各グループをソートする必要があります。再度、感謝します。 – DraganS
@DraganSはフィールド 'd'にインデックスをキャストします。したがって、mongoは毎回重いステージを実行しません – evilive