b***@163.com
b***@163.com
  • 发布:2020-09-25 20:54
  • 更新:2020-09-27 15:47
  • 阅读:1327

uniCloud开发lookup怎么对数组进行排序

分类:uniCloud

例如,下面这个代码会新增返回一个bookList数组,数组中的每一个项有_id, createTime等字段,那么怎么把bookList数组中的数据根据createTime降序排列呢?

var db = cloud.database()
var $ = db.command.aggregate
db.collection('orders').aggregate()
.lookup({
from: "books",
localField: "book",
foreignField: "title",
as: "bookList"
})
.end()
.then(res => console.log(res))
.catch(err => console.error(err))

2020-09-25 20:54 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

使用拼接子查询的方式 pipeline

b***@163.com

b***@163.com (作者) - Less is more.

是的,已解决,感谢!

.lookup({  
            from: 'reply',  
            let: {  
                post_id: '$postId',  
                comment_id: '$_id'  
            },  
            pipeline: $.pipeline()  
                .match(_.expr($.and([  
                    $.eq(['$postId', '$$post_id']),  
                    $.eq(['$commentId', '$$comment_id'])  
                ])))  
                .sort({  
                    createTime: -1  
                })  
                .done(),  
            as: 'replyArray',  
        })

该问题目前已经被锁定, 无法添加新回复