MongoDB Aggregration Framework和Java驱动程序使$或condion条件起作用

编程入门 行业动态 更新时间:2024-10-28 09:15:47
本文介绍了MongoDB Aggregration Framework和Java驱动程序使$或condion条件起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究一种使用聚合器框架查询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条件起作用

本文发布于:2023-11-22 19:00:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:驱动程序   条件   Aggregration   MongoDB   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!