我正在研究一种使用聚合器框架查询mongoDB的方法.我已经建立了聚合,但仍然收到以下异常:
I am working on a method that queries a mongoDB using the aggregator framework. I have built up the aggregate but I keep getting the following exception:
Pipeline :: run():无法识别的管道操作\"$ or"
Pipeline::run(): unrecognized pipeline op \"$or"
如果我正确理解在向查询追加或添加DBObject时将它们隐式添加为和操作.我现在可能真的很累,但是我无法想到使用聚合框架实现一两个条件的方法.
If I understand correctly when you append or add DBObjects to the query they are implicitly added as and operations. I may be really tired right now but I can't think of a way to or two conditions with the aggregation framework.
以下是我的代码段:
DBObject matchCriteriaTransmitter = new BasicDBObject("$match", new BasicDBObject("someKey": "someValue"). append("someKey": "someValue")); DBObject matchCriteriaReceiver = new BasicDBObject("$match", new BasicDBObject("someKey": "someValue"). append("someKey": "someValue")); BasicDBList or = new BasicDBList(); or.add(matchCriteriaTransmitter); or.add(matchCriteriaReceiver); DBObject matchCriteria = new BasicDBObject("$or", or); DBObject sortCriteria = new BasicDBObject("$sort", new BasicDBObject("compoundIndex.scenarioDtg", -1)); DBObject limitCriteria = new BasicDBObject("$limit", 1); DBCollection collection = dao.getCollection(); AggregationOutput output = collection.aggregate(matchCriteria, sortCriteria, limitCriteria);任何见解都将不胜感激!
Any insight is greatly appreciated!
推荐答案检查您可能使用$或作为管道运算符的文档,但没有此类运算符: DOC
Check the docs you likely to use $or as a pipeline operator but there is no such operator: DOC
相反,您可以构造一个$ match运算符,该运算符的内部可以包含or子句
INstead you can construct an $match operator which is inside can contain an or clause
在shell中是这样的:
Something like this in shell:
db.collection.aggregate({$match:{$or:[{someKey:'someValue'},{someOtherKey:'someOtherValue'}]}})您需要更改的是在JAVA中这样的事情:
What you have to change is something like this in JAVA:
更改此:
DBObject matchCriteria = new BasicDBObject("$or", or);对此:
DBObject orCriteria = new BasicDBObject("$or", or); DBObject matchCriteria = new BasicDBObject("$match", orCriteria);更多推荐
MongoDB Aggregration Framework和Java驱动程序使$或condion条件起作用
发布评论