使用多个字段的mongodb查找不起作用

Jai_Arl

我正在尝试使用regCodes集合的siteId从titleInfo集合中获取其他信息

我有两个收藏

regCodes

{
    "siteId" : "123A",
    "registration_code" : "ABC",
    "used_flag" : true,
    "Allowed_Use" : 1,
    "Remaining_Use" : 0,
    "BatchId" : "SNGL",
    "CodeDuration" : 180
}

titleInfo

{
    "title" : "Principles of Microeconomics",
    "product_form_detail" : "EPUB",
    "final_binding_description" : "Ebook",
    "vitalsource_enabled" : false,
    "reading_line" : "with InQuizitive and Smartwork5",
    "volume" : "",
    "protected_content" : {
        "ebookSiteIds" : [ 
            "123A"
        ],
        "studySpaceSiteIds" : [],
        "iqSiteIds" : []
    }
}

下面的查询不起作用,将“ regcodeData”作为空数组。

使用mongodb 3.6.18版

db.getCollection('regCodes').aggregate([
  {
    $match: {
      registration_code: 'ABC'
    }
  },
  {
    $lookup: {
      from: "titleInfo",
      let: {
        regcode_siteId: "$siteId"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $or: [
                {
                  $eq: [
                    "$protected_content.ebookSiteIds",
                    "$$regcode_siteId"
                  ]
                },
                {
                  $eq: [
                    "$protected_content.studySpaceSiteIds",
                    "$$regcode_siteId"
                  ]
                },
                {
                  $eq: [
                    "$protected_content.iqSiteIds",
                    "$$regcode_siteId"
                  ]
                }
              ]
            }
          }
        }
      ],
      as: "regcodeData"
    }
  }
])

下面的查询按预期工作

db.getCollection('titleInfo').find({
  $or: [
    {
      "protected_content.ebookSiteIds": "123A"
    },
    {
      "protected_content.studySpaceSiteIds": "123A"
    },
    {
      "protected_content.iqSiteIds": "123A"
    }
  ]
})
ngShravil.py

您只需使用选项设置为的$unwind运算符来展开数组preserveNullAndEmptyArraystrue

更新的查询:

db.regCodes.aggregate([
  {
    $match: {
      registration_code: "ABC"
    }
  },
  {
    $lookup: {
      from: "titleInfo",
      let: {
        regcode_siteId: "$siteId"
      },
      pipeline: [
        {
          $unwind: {
            path: "$protected_content.ebookSiteIds",
            preserveNullAndEmptyArrays: true
          }
        },
        {
          $unwind: {
            path: "$protected_content.studySpaceSiteIds",
            preserveNullAndEmptyArrays: true
          }
        },
        {
          $unwind: {
            path: "$protected_content.iqSiteIds",
            preserveNullAndEmptyArrays: true
          }
        },
        {
          $match: {
            $expr: {
              $or: [
                {
                  $eq: [
                    "$protected_content.ebookSiteIds",
                    "$$regcode_siteId"
                  ]
                },
                {
                  $eq: [
                    "$protected_content.studySpaceSiteIds",
                    "$$regcode_siteId"
                  ]
                },
                {
                  $eq: [
                    "$protected_content.iqSiteIds",
                    "$$regcode_siteId"
                  ]
                }
              ]
            }
          }
        }
      ],
      as: "regcodeData"
    }
  }
])

MongoPlayGroundLink

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Data MongoDB具有多个字段的推送操作不起作用

使用MvvmCross在SQLite插件上使用多个字段的linq查询“ orderby”不起作用

Magento-addAttributeToSelect对于多个字段不起作用

尝试在Mongodb中同时使用$ regex查找多个字段

jQuery-在多个字段上应用多个事件-不起作用

Mongodb-PHP:使用'$ and'函数查找查询不起作用

使用管道的 MongoDB 聚合查找不起作用

在多个字段中执行&符在ruby中不起作用

ArcGIS JS API搜索源searchFields-多个字段不起作用

mongodb 查找查询不起作用

MongoDB匹配多个条件不起作用

使用 str_replace 替换多个字符串不起作用

在MongoDB中的多个字段上使用$ in

RegExp在MongoDB中为多个字段查找匹配的文档

mongodb通过多个字段查找组

BPF 过滤查找字段在某些阶段不起作用

Excel:SMALL(),IF()和ROW()使用多个条件查找值的行号-AND()不起作用

当我使用多个文件上传时,字段不起作用

通过_id进行Mongodb聚合查找不起作用

查找表上的 Mongodb 匹配不起作用

查找聚合中的管道在mongodb中不起作用

具有多个值的参数字段不起作用

更改多个文档字段在ES 2.3.3中不起作用

jQuery添加多个输入字段不起作用

按字段分组在MongoDB中不起作用

使用 MongoDB 驱动程序删除多个文档不起作用

Laravel 5.7使用3个字段的required_with和required_with_all验证不起作用

猫鼬使用多个字段查找和排序

查找类不起作用