简体   繁体   English

在@Query Spring data mongo 中使用 OR 时的问题

[英]Issues when using OR in @Query Spring data mongo

I have following method in my repository:我的存储库中有以下方法:

@Query(value = "{ '$or': [?0 ]}") List<MyObject> findByObject(String lookupKeyValuePairs);

I'm passing following String as input to this method.我将以下字符串作为输入传递给此方法。

String: { 'keyone': 'one','keytwo': 'one' },{ 'keyone': 'two','keytwo': 'two' }字符串: { 'keyone': 'one','keytwo': 'one' },{ 'keyone': 'two','keytwo': 'two' }

I expect the query to be { '$or': [ { 'keyone': 'one','keytwo': 'one' },{ 'keyone': 'two','keytwo': 'two' } ]} but spring is evaluating above query as我希望查询是{ '$or': [ { 'keyone': 'one','keytwo': 'one' },{ 'keyone': 'two','keytwo': 'two' } ]}但 spring 将上述查询评估为

org.springframework.data.mongodb.core.MongoTemplate - find using query: { "$or": ["{ 'keyone': 'one','keytwo': 'one' },{ 'keyone': 'two','keytwo': 'two' }"] } fields: Document{{}} for class: MyClass in collection: MyCollection

and throwing following exception: Caused by: com.mongodb.MongoQueryException: Query failed with error code 2 and error message '$or/$and/$nor entries need to be full objects' on server并引发以下异常: Caused by: com.mongodb.MongoQueryException: Query failed with error code 2 and error message '$or/$and/$nor entries need to be full objects' on server

It's dynamic parameters.是动态参数。 The key value pairs are gonna change every time.键值对每次都会改变。 That's why i'm passing the whole thing as a string.这就是为什么我将整个事情作为一个字符串传递。

Can someone help to point out where the issue is?有人可以帮忙指出问题出在哪里吗?

can you pass them as separate params?您可以将它们作为单独的参数传递吗?

@Query("{ '$or' : [ { 'keyone': ?0,'keytwo': ?1},{ 'keyone': ?2,'keytwo': ?3 } ]}") 
List<MyObject> findByObject(String param0, String param1, String param2, String param3);

repo.findByObject('one', 'one', 'two', 'two');

or或者

@Query("{ '$or' : [ { ?0: ?1, ?2: ?3},{ ?4: ?5, ?6: ?7 } ]}") 
List<MyObject> findByObject(String param0, String param1, String param2, String param3, String param4, String param5, String param6, String param7);

repo.findByObject('keyone', 'one', 'keytwo', 'one','keyone', 'two', 'keytwo', 'two');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM