【问题标题】:Transform field in MongoDB aggregateMongoDB聚合中的转换字段
【发布时间】:2021-08-01 07:31:56
【问题描述】:

我通过聚合对 mongodb 进行了一些查询,我可以得到我想要的结果并发送给客户端。 在此之后,我需要合并文件名和文件 url。

我的结果

{
  "_id" : "1", "name" : "x", "image" : "y.jpg"
}

但我想编辑这样的结果

{
  "_id" : "1", "name" : "x", "image" : "http:/x.com/y.jpg"
}

我可以在循环聚合后编辑结果。但是可以根据需要聚合给我吗?

【问题讨论】:

    标签: mongodb mongoose mongodb-query nosql aggregate


    【解决方案1】:

    演示 - https://mongoplayground.net/p/nWv7d2Ld5sy

    使用$set$concat

       db.collection.aggregate([
          {
            $set: {
              image: {
                $concat: [ "http://", "$name", ".com/", "$image" ] // combine to form the output
              }
            }
          }
        ])
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    相关资源
    最近更新 更多