MongoDB查询标准MongoDB转换抛出异常

发布于 2025-02-10 07:38:26 字数 2226 浏览 4 评论 0原文

我得到了这样的蒙哥查询:

{$and:
[
      {$or:
      [
           {"itemNbr": { $in: :#{#itemNbrs} }},
           {"deptNbr": { $in: :#{#deptNbrs} }} //I haven't provided the criteria code bellow.
      ]},
      {"allocationPlanType": :#{#allocationPlanType}},
      {$or:
      [
           {"overallNeedQuantity": { $gte: :#{#showAllocationPlan} }},
           {"overallPlannerQuantity": { $gte: :#{#showAllocationPlan} }}
      ]}
      {$or:
      [
           {"itemDesc1": { $eq: :#{#itemName} }},
           {"itemDesc2": { $gte: :#{#itemName} }}
      ]}
]}

我已经写了这样的标准查询:

List<Criteria> andExpression = new ArrayList<>();
if (!CollectionUtils.isEmpty(itemNbrs)) {
            itemNbrQuery = Criteria.where("itemNbr").in(itemNbrs);
            andExpression.add(itemNbrQuery);
        }

 andExpression.add(Criteria.where("allocationPlanType").is(allocationPlanType));
    if(Objects.nonNull(itemName))
    {
        itemNameQuery=criteria.orOperator(
                Criteria.where("itemDesc1").is(itemName),
                Criteria.where("itemDesc2").is(itemName)
        );

        andExpression.add(itemNameQuery);
    }
  if (Objects.isNull(choice)) {
            Criteria noFltersQuery = criteria.orOperator(
                    Criteria.where("overallNeedQuantity").gte(showAllocationPlan),
                    Criteria.where("overallPlannerQuantity").gte(showAllocationPlan)
            );
            andExpression.add(noFltersQuery);
            
    }else {
        Criteria noFltersQuery = 
        Criteria.where("overallPlannerQuantity").gte(showAllocationPlan);
        andExpression.add(noFltersQuery);
    }
    query.with(pageableRequest);
    query.addCriteria(criteria.andOperator(andExpression.toArray(new Criteria[andExpression.size()])));

现在,它抛出了这个例外:

由于org.bson.document的限制,您无法添加第二个 指定为'$或: [document {{resutionneedquantity = document {{$ gte = 1}}}}}, document {{总体plannerquantity = document {{$ gte = 1}}}}}}}'。标准 已经包含'$或:[document {{itemDesc1 = comp curr prek workbook}},document {{itemDesc2 = comp curr prek workbook}}]'。

现在,我知道这个例外在MongoDB中非常普遍,但是我并不知道如何根据我需要避免这种例外的情况来构建标准查询。

I got a Mongo query like this:

{$and:
[
      {$or:
      [
           {"itemNbr": { $in: :#{#itemNbrs} }},
           {"deptNbr": { $in: :#{#deptNbrs} }} //I haven't provided the criteria code bellow.
      ]},
      {"allocationPlanType": :#{#allocationPlanType}},
      {$or:
      [
           {"overallNeedQuantity": { $gte: :#{#showAllocationPlan} }},
           {"overallPlannerQuantity": { $gte: :#{#showAllocationPlan} }}
      ]}
      {$or:
      [
           {"itemDesc1": { $eq: :#{#itemName} }},
           {"itemDesc2": { $gte: :#{#itemName} }}
      ]}
]}

I have written a criteria query like this:

List<Criteria> andExpression = new ArrayList<>();
if (!CollectionUtils.isEmpty(itemNbrs)) {
            itemNbrQuery = Criteria.where("itemNbr").in(itemNbrs);
            andExpression.add(itemNbrQuery);
        }

 andExpression.add(Criteria.where("allocationPlanType").is(allocationPlanType));
    if(Objects.nonNull(itemName))
    {
        itemNameQuery=criteria.orOperator(
                Criteria.where("itemDesc1").is(itemName),
                Criteria.where("itemDesc2").is(itemName)
        );

        andExpression.add(itemNameQuery);
    }
  if (Objects.isNull(choice)) {
            Criteria noFltersQuery = criteria.orOperator(
                    Criteria.where("overallNeedQuantity").gte(showAllocationPlan),
                    Criteria.where("overallPlannerQuantity").gte(showAllocationPlan)
            );
            andExpression.add(noFltersQuery);
            
    }else {
        Criteria noFltersQuery = 
        Criteria.where("overallPlannerQuantity").gte(showAllocationPlan);
        andExpression.add(noFltersQuery);
    }
    query.with(pageableRequest);
    query.addCriteria(criteria.andOperator(andExpression.toArray(new Criteria[andExpression.size()])));

Now its throwing this exception :

Due to limitations of the org.bson.Document, you can't add a second
'$or' expression specified as '$or :
[Document{{overallNeedQuantity=Document{{$gte=1}}}},
Document{{overallPlannerQuantity=Document{{$gte=1}}}}]'. Criteria
already contains '$or : [Document{{itemDesc1=COMP CURR PREK
WORKBOOK}}, Document{{itemDesc2=COMP CURR PREK WORKBOOK}}]'.

Now I know this exception is very common in MongoDB but I am not getting clue how to construct the Criteria query as per my need to avoid this exception.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文