有没有办法在 MongoDB 中使用聚合管道搜索嵌入式数组?

西兰花

我需要搜索类别字段中的所有数组以查找产品是否属于“T 恤”类别,有没有办法搜索类别字段中的所有数组?

任何帮助将不胜感激。

这是我迄今为止尝试过的

// Aggregate Pipeline
db.products.aggregate([
  {$group :
    {_id :
      {productID : '$productID', productTitle : '$productTitle ', categories: '$categories'
    }
  }
},
  {$project :
      {
          category_tshirts : {$in : ['T-Shirts', '$_id.categories']}
      }
   }
]);

以下是其中一份文件的示例:

// Example document
{
        "productID" : "B000072SJ2",
        "productTitle" : "Loosegear Short Sleeve",
        "categories" : [ 
            [ 
                "Sports & Outdoors", 
                "Clothing", 
                "Men", 
                "Shirts", 
                "T-Shirts"
            ], 
            [ 
                "Clothing, Shoes & Jewelry", 
                "Men", 
                "Clothing", 
                "Shirts"
            ], 
            [ 
                "Clothing, Shoes & Jewelry", 
                "Men", 
                "Big & Tall"
            ]
        ]
    }
阿尔乔姆·阿尔希波夫

当然。您可以使用unwind运算符来摆脱嵌套数组。

此运算符允许您拥有与所选数组中的项目一样多的单独文档。例如,有这个文件:

{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
     [ 
       "Sports & Outdoors", 
       "Accessories", 
       "Sport Watches"
     ], 
     [ 
       "Clothing, Shoes & Jewelry", 
       "Sport Watches"
     ], 
     [ 
        "Clothing, Shoes & Jewelry", 
        "Men"
     ]
   ]
 }

如果您在categories场上使用放松

db.products.aggregate( [ { $unwind : "$categories" } ] )

您将获得 3 个几乎相似的文档,其中只有类别不同(顶级数组中的每个项目有 1 个文档):

{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
       "Sports & Outdoors", 
       "Accessories", 
       "Sport Watches"
     ]
 },
{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
       "Clothing, Shoes & Jewelry", 
       "Sport Watches"
     ]
 },
{
  "productID" : "B00006I551",
  "productTitle" : "CASIO F91W-1 Casual Sport Watch",
  "categories" : [ 
        "Clothing, Shoes & Jewelry", 
        "Men"
     ]
 }

现在您可以使用$in运算符或其他方式通过categories.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法在MongoDB中使用聚合查询调用自定义函数

有没有办法使用 PyMongo 在我的 kivy 程序中嵌入 MongoDB 图表?

有没有办法在同一项目中使用MongoDB或MySQL?

有没有办法在 mongodb 全文搜索中转义重音

有没有办法在 Prisma for MongoDB 的模式中显式声明 int32?

有没有办法检查 MongoDB 中数组值中的重复条目?

有没有办法在 mongoDB 的嵌套数组中查找文档

有没有办法使用 in Array 函数从 mongodb 集合中获取数据。如果数组 id 是字符串

有没有办法在mongodb的子文档数组中匹配2个特定的字段值?

有没有办法在MongoDB中返回文档中数组的一部分?

使用 MongoDb Java 驱动程序有没有办法动态获取过滤器功能

有没有办法使用最近获得的价值来寻找其他价值?在MongoDB中

使用.net core和mongodb,有没有办法只更新我在json中传递的字段?

有没有办法用MongoDB替换Many()?

有没有办法在带有数组的对象中搜索mongodb中的值,并且该值可以存在于该数组的任何对象中

使用Discord.py,有没有办法读取嵌入式消息?

有没有办法使用FOR JSON返回字符串或嵌入式JSON?

有没有办法在querySelector中使用*?

有没有办法在watchOS中使用`useProtocolCachePolicy`?

有没有办法在gnuplot中使用通配符?

有没有办法在BottomSheetDialogFragment 中使用AlertDialog?

有没有办法在Python中使用PhantomJS?

有没有办法在Firestore中使用GeoFire?

有没有办法在GLSL中使用联合?

有没有办法在JavaScript中使用C ++?

有没有办法在PostgreSQL中使用qualify

有没有办法在React中使用Wavify?

有没有办法检索ADT?似乎没有办法使用DDMTemplateLocalServiceUtil

有没有办法在Outlook电子邮件中使用嵌入式PNG图像?