如何在单个阶段管道中将嵌套数组中的直接键值对放置,而不使用$unwind?

huangapple go评论76阅读模式
英文:

mongo query : How can a direct key-value pair be placed from a nested array in a single stage pipeline and without the use of $unwind?

问题

{
"priceDetails": {
"cityId": "64a297a8bb1dc440ccef6c3c",
"name": "Bhopal",
"prices": "1200",
"commissionType": "PERCENTAGE",
"commissionValue": "20",
"_id": "64a2990b344e735476fb54ca"
}
}

英文:

I have something like this.

{
  "priceDetails": [
    {
      "cities": [
        {
          "cityId": "64a297a8bb1dc440ccef6c3c",
          "name": "Bhopal",
          "prices": "1200",
          "commissionType": "PERCENTAGE",
          "commissionValue": "20",
          "_id": "64a2990b344e735476fb54ca"
        }
      ]
    }
  ]
}

Need something like this.

{
  "priceDetails": {
    "cityId": "64a297a8bb1dc440ccef6c3c",
    "name": "Bhopal",
    "prices": "1200",
    "commissionType": "PERCENTAGE",
    "commissionValue": "20",
    "_id": "64a2990b344e735476fb54ca",
  }
}

I tried $unwind, but it uses a lot of database processing power to filter and retrieve the data.

I'm using node v18.7.0 & mongo version v4.4

I'm baffled at this point, any help would be greatly appreciated. Cheers!

答案1

得分: 0

一种选择是使用简单的投影:

db.collection.find(
  {},
  {
    _id: 0,
    priceDetails: {$first: "$priceDetails.cities"}
  }
)

playground example上查看它的运行情况。

英文:

One option is to use a simple projection:

db.collection.find(
  {},
  {
    _id: 0,
    priceDetails: {$first: "$priceDetails.cities"}
  }
)

See how it works on the playground example

答案2

得分: 0

在你目前用于聚合数据的流程中,你可以使用 $set 和 $arrayElemAt。

要修改响应,请使用 $set,要从数组中检索元素,请使用 $arrayElemAt。

{
  $set: {
    priceDetails: {
      $arrayElemAt: [
        {
          $arrayElemAt: ["$priceDetails.cities", 0]
        },
        0
      ]
    }
  }
}

数组的第一个索引将由内部的 $arrayElemAt 设置在外部数组的 0 索引位置。然后,外部的 $arrayElemAt 将以与内部的 $arrayElemAt 类似的方式运行。

请查阅文档以获取更多详细信息。

$set : 链接

$arrayElemAt : 链接

希望这对你或其他人有所帮助。

英文:

In your present pipeline for aggregating data, you can utilise $set and $arrayElemAt.

To modify the response, use $set, and to retrieve an element from an array, use $arrayElemAt.

{
  $set: {
    priceDetails: {
      $arrayElemAt: [
        {
          $arrayElemAt: ["$priceDetails.cities", 0]
        },
        0
      ]
    }
  }
}

The first index of the array will be set by inner $arrayElemAt. in the outer array's 0 index. The outer $arrayElemAt will then act in a manner similar to that of the inner $arrayElemAt.

Consult the document for more details.

$set : https://www.mongodb.com/docs/manual/reference/operator/update/set/#definition

$arrayElemAt : https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayElemAt/

I hope that this may be useful to you or someone else.

huangapple
  • 本文由 发表于 2023年7月10日 21:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定