MongoDB查询标准MongoDB转换抛出异常
我得到了这样的蒙哥查询:
{$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论