1

如何使用聚合来实现这一点?

以下是我的数据:

{
  "FirstName": null,
  "LastName": "test1",
  "Gender": null,
  "IsActive": true,
},
{
  "FirstName": "test2",
  "LastName": "jagema",
  "Gender": "Female",
  "IsActive": true,
},

我希望最终的 OP 是:

{
  "FullName": "abiy Jagema",
  "Gender": "Female",
  "IsActive": true,
},
{
  "FullName": Natra,
  "Gender": null,
  "IsActive": true,
},

因此,如果名字和姓氏都存在 concat else 只需将姓氏设置为全名

4

1 回答 1

0

试试下面的查询:

db.aggtest.aggregate([ 
    { $match: { IsActive: true } }, 
    { $project: { 
        "_id": 0,
        "Gender": 1, 
        "IsActive": 1, 
        "FullName": { 
           $concat: [ 
              { $ifNull: [ "$FirstName", "" ]}, 
              " ",
              { $ifNull: [ "$LastName", "" ]}
           ]
        } 
    }} 
])
于 2019-09-10T09:08:13.510 回答