【问题标题】:formatted response for a mongo querymongo 查询的格式化响应
【发布时间】:2020-10-01 01:30:20
【问题描述】:

我看到一篇帖子,我们可以使用查询:db.collection.find({"pc.pcId": "2"}) 并根据pcId 获取记录。 结果:

{
  "name" : "user",
  "number":"09xxxxxxx21",
  "pc" : [{
      "pcId" : "1",
      "pcName" : "Desktop",
      "pcOwner" : "user1"
    }, {
      "pcId" : "2",
      "pcName" : "Laptop",
      "pcOwner" : "user1"
    }
  ]}
}

我的问题是如果我想要这样的结果:

   {
      "pcId" : "2",
      "pcName" : "Desktop",
      "pcOwner" : "user1"
   }

仅记录我正在运行查询的特定结果。 有什么疑问吗?

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    您可以通过聚合来实现这一点。有几种方法

    1. $unwind 扁平化数组
    2. 使用$match匹配对象
    3. $replaceRoot 帮助用对象替换整个文档

    Mongo 脚本如下

    db.collection.aggregate([
      {
        "$unwind": "$pc"
      },
      {
        $match: {
          "pc.pcId": "2"
        }
      },
      {
        "$replaceRoot": {
          "newRoot": "$pc"
        }
      }
    ])
    

    工作Mongo playground


    第二种方式

    1. 使用$filter过滤期望输出
    2. 获得过滤结果后,您可以使用$arrayElemAt 获得所需输出的数组的第一个元素
    3. 如果在 0 索引处没有元素,这将引发错误。为了防止它,我们使用$ifNull
    4. $replaceRoot 帮助用对象替换整个文档

    mongo 脚本是

    [
      {
        $project: {
          pc: {
            $ifNull: [
              {
                $arrayElemAt: [
                  {
                    $filter: {
                      input: "$pc",
                      cond: {
                        $eq: [
                          "$$this.pcId",
                          "1"
                        ]
                      }
                    }
                  },
                  0
                ]
              },
              []
            ]
          }
        }
      },
      {
        "$replaceRoot": {
          "newRoot": "$pc"
        }
      }
    ]
    

    工作Mongo playground

    【讨论】:

      【解决方案2】:
      > db.version();
      4.2.6
      > db.users.find().pretty();
      {
              "_id" : ObjectId("5f74aebe377e73757bb7cead"),
              "name" : "user",
              "number" : "09xxxxxxx21",
              "pc" : [
                      {
                              "pcId" : "1",
                              "pcName" : "Desktop",
                              "pcOwner" : "user1"
                      },
                      {
                              "pcId" : "2",
                              "pcName" : "Laptop",
                              "pcOwner" : "user1"
                      }
              ]
      }
      > db.users.aggregate([
      ... {$unwind:"$pc"},
      ... {$match:{"pc.pcId":"2"}},
      ... {$project:{
      ...     "_id":0,
      ...     "pc.pcId":1,
      ...     "pc.pcName":1,
      ...     "pc.pcOwner":1}}
      ... ]).pretty();
      { "pc" : { "pcId" : "2", "pcName" : "Laptop", "pcOwner" : "user1" } }
      >
      

      【讨论】:

        猜你喜欢
        • 2021-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多