mongodb aggregate 聚合 操作(扁平化flatten)

mongodb自带的函数非常多,最近用mongo做持久化数据库,遇到一个需求:子文档是个数组,把数组里的各个字段扁平化合到根文档中,查过资料后(主要是mongodb的文档和stackoverflow)写出了下面的语句

 1 db.getCollection('table').aggregate
 2 ([ {$match:{"a" : {$gt:"2018-01-01 08:24:35"}}},
 3     { $unwind:  {path: '$fillDocArray(subDoc)'}},
 4     { $project: { 'b':1,
 5         'c':1,
 6         'd':1,
 7         'e': 1,
 8         another_a: '$subDoc.subfield1', 
 9         another_b:'$subDoc.subfield'} }
10 ])

猜你喜欢

转载自www.cnblogs.com/peng18/p/10816178.html