gpt4 book ai didi

java - 如何在单个查询对象 Mongodb Spring 数据中添加 or & 和 Criteria 子句

转载 作者:可可西里 更新时间:2023-11-01 09:59:08 27 4
gpt4 key购买 nike

我想同时向我的查询对象添加 or & 和子句,但我一直收到以下错误

由于 com.mongodb.BasicDBObject 的限制,您不能添加第二个“空”条件吗?

例如

query.addCriteria(new Criteria().orOperator(Some Critera);


query.addCriteria(new Criteria().andOperator(Some Critera);

谁能帮我解决这个问题?

详细信息:

实际上我正在尝试解析以下 json 并基于解析后的 json 构建动态查询

{
"query":{

"where":[{

"or":[
{
"fieldName":"address1","fieldValue":"Dummy address1",

"operator":"equal"
}

],
"and":[{
"fieldName":"version","fieldValue":"1",

"operator":"equal"

}]
}
]
}
}

所以你可以建议我使用 mongoTemplate Spring 数据将该 json 解析为 Mongodb 查询的其他方法

这是我的解析代码

if(null != eventSearch.getQuery())
{

if(null != eventSearch.getQuery().getWhere() && eventSearch.getQuery().getWhere().size() > 0)
{

for (Where whereClause : eventSearch.getQuery().getWhere()) {

if(null != whereClause.getOr() && whereClause.getOr().size() > 0){

List<org.springframework.data.mongodb.core.query.Criteria> orCriterias = new ArrayList<org.springframework.data.mongodb.core.query.Criteria>(whereClause.getOr().size());

for (Field field: whereClause.getOr()) {

if(field.getOperator().equalsIgnoreCase(QueryOperator.IS))
{
orCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).is(field.getFieldValue()));

}else if(field.getOperator().equalsIgnoreCase(QueryOperator.LT)){

orCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).lt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GT)){

orCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).gt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.LTE)){

orCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).lte(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GTE)){

orCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).gte(field.getFieldValue()));
}
}

query.addCriteria(new org.springframework.data.mongodb.core.query.Criteria().orOperator(orCriterias.toArray(new org.springframework.data.mongodb.core.query.Criteria[whereClause.getOr().size()])));
}

if(null != whereClause.getAnd() && whereClause.getAnd().size() > 0){

List<org.springframework.data.mongodb.core.query.Criteria> andCriterias = new ArrayList<org.springframework.data.mongodb.core.query.Criteria>(whereClause.getAnd().size());

for (Field field: whereClause.getAnd()) {

if(field.getOperator().equalsIgnoreCase(QueryOperator.IS))
{
andCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).is(field.getFieldValue()));

}else if(field.getOperator().equalsIgnoreCase(QueryOperator.LT)){

andCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).lt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GT)){

andCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).gt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.LTE)){

andCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).lte(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GTE)){

andCriterias.add(org.springframework.data.mongodb.core.query.Criteria.where(field.getFieldName()).gte(field.getFieldValue()));
}
}
//Getting exception at this line
query.addCriteria(new org.springframework.data.mongodb.core.query.Criteria().andOperator(andCriterias.toArray(new org.springframework.data.mongodb.core.query.Criteria[whereClause.getAnd().size()])));
}
}}

最佳答案

你的代码太乱了,我换成了org.springframework.data.mongodb.core.query.CriteriaCriteria暂时。

    // added
Query query = new Query();

if(null != eventSearch.getQuery())
{

if(null != eventSearch.getQuery().getWhere() && eventSearch.getQuery().getWhere().size() > 0)
{
// added
List<Criteria> wheres = new ArrayList<>();

for (Where whereClause : eventSearch.getQuery().getWhere()) {

// added
Criteria where = new Criteria();

if(null != whereClause.getOr() && whereClause.getOr().size() > 0){

List<Criteria> orCriterias = new ArrayList<Criteria>(whereClause.getOr().size());

for (Field field: whereClause.getOr()) {

if(field.getOperator().equalsIgnoreCase(QueryOperator.IS))
{
orCriterias.add(Criteria.where(field.getFieldName()).is(field.getFieldValue()));

}else if(field.getOperator().equalsIgnoreCase(QueryOperator.LT)){

orCriterias.add(Criteria.where(field.getFieldName()).lt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GT)){

orCriterias.add(Criteria.where(field.getFieldName()).gt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.LTE)){

orCriterias.add(Criteria.where(field.getFieldName()).lte(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GTE)){

orCriterias.add(Criteria.where(field.getFieldName()).gte(field.getFieldValue()));
}
}

// comment out
// query.addCriteria(new Criteria().orOperator(orCriterias.toArray(new Criteria[whereClause.getOr().size()])));

// replaced with
if (orCriterias.size() > 0) {
where.orOperator(orCriterias.toArray(new Criteria[0]));
}
}

if(null != whereClause.getAnd() && whereClause.getAnd().size() > 0){

List<Criteria> andCriterias = new ArrayList<Criteria>(whereClause.getAnd().size());

for (Field field: whereClause.getAnd()) {

if(field.getOperator().equalsIgnoreCase(QueryOperator.IS))
{
andCriterias.add(Criteria.where(field.getFieldName()).is(field.getFieldValue()));

}else if(field.getOperator().equalsIgnoreCase(QueryOperator.LT)){

andCriterias.add(Criteria.where(field.getFieldName()).lt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GT)){

andCriterias.add(Criteria.where(field.getFieldName()).gt(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.LTE)){

andCriterias.add(Criteria.where(field.getFieldName()).lte(field.getFieldValue()));
}
else if(field.getOperator().equalsIgnoreCase(QueryOperator.GTE)){

andCriterias.add(Criteria.where(field.getFieldName()).gte(field.getFieldValue()));
}
}

// comment out
// //Getting exception at this line
// query.addCriteria(new Criteria().andOperator(andCriterias.toArray(new Criteria[whereClause.getAnd().size()])));

// replaced with
if (andCriterias.size() > 0) {
where.andOperator(andCriterias.toArray(new Criteria[0]));
}

}

// added
wheres.add(where);
}
// added
if (wheres.size() > 0) {
query.addCriteria(new Criteria().andOperator(wheres.toArray(new Criteria[0])));
}
}
}

如果您的运营商只包括 == != < <= > >= ,您可以尝试通过直接或间接更改 JSON 来缩短代码,如下所示:

{
"query":{

"where":[{

"or":[
{
"fieldName":"address1","fieldValue":"Dummy address1",

"operator":"equal"
},
{
"fieldName":"address1","fieldValue":"Dummy address2",

"operator":"$gt" // added: greater than
},
{
"fieldName":"address1","fieldValue":"Dummy address3",

"operator":"$ne" // added: not equal
}

],
"and":[{
"fieldName":"version","fieldValue":"1",

"operator":"equal"

}]
}
]
}
}

那么,部分代码可以这样写:

  for (Field field: whereClause.getAnd()) {

if(field.getOperator().equalsIgnoreCase(QueryOperator.IS))
{
andCriterias.add(Criteria.where(field.getFieldName()).is(field.getFieldValue()));

} else {
andCriterias.add(Criteria.where(field.getOperator()).is(new BasicDBObject(field.getFieldName(), field.getFieldValue())));
}
}

whereClause.getOr()类似于 whereClause.getAnd() , 然后您可以将它们组合在一个循环中以根据需要缩短代码。

关于java - 如何在单个查询对象 Mongodb Spring 数据中添加 or & 和 Criteria 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25425577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com