聚合查询lookup如何排序呢?

发布于 8 年前作者 gdeng12309 次浏览最后编辑 8 年前来自 issues

尝试着在collection()后边加.orderBy,然后就会在调用该云函数的地方报一个错:

2 回复
fxue
fxue1 楼6 年前

orderBy不能用于聚合,

聚合里是sort。

dshen
dshen2 楼6 年前

db.collection(‘table1’).aggregate()

.lookup({

    from: ‘table2’,

    pipeline: $.pipeline()

        …

        .sort({

            age: -1 // age倒序

        })

        .done(),

    as: ‘rows2’,

})

 .sort({

    age: -1 // age倒序

})

.end()