已搞定 db.collection('Program').aggregate() .match(_.and([{ valid: true }])) .project({ remarks: 1,_id:0 }) .unwind({ path:'$remarks' }) .replaceRoot({ newRoot: $.mergeObjects('$remarks') }) .sort({pldzs:-1}) .limit(3) .end()
云开发数据库取子集的前N条?[ { "_id": "1f154439638e97330137f2475dec9ffc", "title": "1", "remarks": [ { "remarktime": "2022/12/06 14:13:12", "id": 1670307282722 }, { "remarktime": "2022/12/06 13:00:10", "id": 1670302838410 }, { "remarktime": "2022/12/06 12:58:19", "id": 1670302752430 }, { "remarktime": "2022/12/06 12:49:31", "id": 1670302182764 }, { "remarktime": "2022/12/06 12:49:09", "id": 1670302197960 }, { "id": 1670298287375, "remarktime": "2022/12/06 11:43:50" } ] }, { ... } ] 只想取得remarks的前三个怎么做?
2022-12-06请各位大佬帮忙看看,感谢
如何将子集合提到上层?//查询语句 db.collection('ProgramOfPerson').aggregate() .match(_.and([{ _openid: '*' }, { like: 1 }])) .project({ programid: 1 }) .lookup({ from: 'Program', let: { id: '$programid' }, pipeline: $.pipeline() .match(_.expr($.and([ $.eq(['$_id', '$$id']), ]))) .project({ _id:0, title: 1, datetime: 1, content: $.substrCP(['$content', 0, 100]) }) .done(), as: 'a' }) .end() [{ "_id": "*", "programid": "*", "a": [ { "datetime": "2022/11/22 12:33:26", "title": "24", "content": "24\n" } ] }] 将上面的转为下面的 [{ "_id": "*", "programid": "*", "datetime": "2022/11/22 12:33:26", "title": "24", "content": "24\n" }]
2022-11-29