mongodb内部查询

发布于 2025-02-08 02:00:54 字数 792 浏览 0 评论 0原文

我有一个格式的mongoDB文档。此处给出的阵列有多个集合。 我如何获得sid = 100的所有元素。

{
  "_id" : "123456", 
    "Continent" : {
        "Country" : [
            [
                US, 
                {
                    "State" : [
                        [
                            100, 
                            {
                                "Product" : "Corn",  
                                "SID" : 100
                            }
                        ], 
                        [
                            200, 
                            {
                                "Product" : "Maze",  
                                "SID" : 200
                            }
                        ]
            ],
                }
            ]
        ], 
    }, 
}

I have a mongodb document in this format. There are multiple collections of arrays as given here.
How do I get all elements where SID = 100.

{
  "_id" : "123456", 
    "Continent" : {
        "Country" : [
            [
                US, 
                {
                    "State" : [
                        [
                            100, 
                            {
                                "Product" : "Corn",  
                                "SID" : 100
                            }
                        ], 
                        [
                            200, 
                            {
                                "Product" : "Maze",  
                                "SID" : 200
                            }
                        ]
            ],
                }
            ]
        ], 
    }, 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2025-02-15 02:00:54

选项1:

db.collection.aggregate([
 {
   $unwind: "$Continent.Country"
 },
 {
   $unwind: "$Continent.Country"
 },
 {
   $unwind: "$Continent.Country.State"
 },
 {
   $unwind: "$Continent.Country.State"
 },
 {
  $match: {
    "Continent.Country.State.SID": 100
 }
 },
 {
  $project: {
    "myProductWithSID100": "$Continent.Country.State"
   }
 }
])

解释:

  1. 放松所有数组和嵌套数组。
  2. 仅匹配SID:100
  3. 项目仅必要对象内容

playground1

选项2:选项2:
(稍微更快)

db.collection.aggregate([
{
  $unwind: "$Continent.Country"
},
{
  $unwind: "$Continent.Country"
 },
{
  $unwind: "$Continent.Country.State"
},
{
  $match: {
    "Continent.Country.State.SID": 100
 }
},
{
 "$addFields": {
  "Continent.Country.State": {
    "$filter": {
      "input": "$Continent.Country.State",
      "as": "c",
      "cond": {
        $eq: [
          "$c.SID",
          100
        ]
       }
     }
    }
  },
  {
    $unwind: "$Continent.Country.State"
  },
  {
    $project: {
      "myProductWithSID100": "$Continent.Country.State"
   }
  }
 ])

解释:

  1. 放开第一个3X阵列
  2. 仅匹配至少具有1X SID的文档:第4
  3. 阵列中的100个量仅具有SID的第四个数组中的100个文档:100
  4. 放开第四阵列
  5. 项目,仅我们需要的最终对象

playground2

Option 1:

db.collection.aggregate([
 {
   $unwind: "$Continent.Country"
 },
 {
   $unwind: "$Continent.Country"
 },
 {
   $unwind: "$Continent.Country.State"
 },
 {
   $unwind: "$Continent.Country.State"
 },
 {
  $match: {
    "Continent.Country.State.SID": 100
 }
 },
 {
  $project: {
    "myProductWithSID100": "$Continent.Country.State"
   }
 }
])

Explained:

  1. Unwind all arrays and nested arrays.
  2. Match only the subobjects with SID:100
  3. Project only the necessary object content

Playground1

Option 2:
( Slightly faster)

db.collection.aggregate([
{
  $unwind: "$Continent.Country"
},
{
  $unwind: "$Continent.Country"
 },
{
  $unwind: "$Continent.Country.State"
},
{
  $match: {
    "Continent.Country.State.SID": 100
 }
},
{
 "$addFields": {
  "Continent.Country.State": {
    "$filter": {
      "input": "$Continent.Country.State",
      "as": "c",
      "cond": {
        $eq: [
          "$c.SID",
          100
        ]
       }
     }
    }
  },
  {
    $unwind: "$Continent.Country.State"
  },
  {
    $project: {
      "myProductWithSID100": "$Continent.Country.State"
   }
  }
 ])

Explained:

  1. Unwind the first 3x arrays
  2. Match only documents having at least 1x SID:100 in 4th array
  3. Filter only those from 4th array having SID:100
  4. Unwind the 4th array
  5. Project only the final object we need

Playground2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文