简体   繁体   中英

Query Mongo DB using Java Driver

So I have a list of queryObjects (a class I created in my program) to query from a mongo DB with expressions all in an object like (pseudo code):

queryObject : { fild, operation, expression }

example : queryObject : { field : "pagePath", operation:"$in", expression:"/home"}

And the user can create as many queries as he/she wants. This works like charm until I have two queries with the same field name, example:

queryObject1 : { field : "pagePath", operation:"$in", expression:"/home"}

queryObject2 : { field : "pagePath", operation:"$regex", expression:"(.html)$"}

than I have: query.put(queryObject1) and query.put(queryObject2) this command:

FindIterable<Document> iterable = statistics.find(query).projection(excludeId());

takes into consideration only the second put, what made me think that maybe it overrides the first. What can I do to prevent this from happening? is there a query syntaxe in Mongo that allows me to test that the Page Path is both a Home Page and ends with .html? knowing that this condition can change I always have to read the Query Object and create a MongoQuery in my program.

Assuming that your query object basically generates a Bson which can be used for Collection.find(), you can combine multiple Bsons via the Filters-utilities:

Bson b1 = firstQuery.convertToDbQuery(); // or however you do it... ;-)
Bson b2 = secondQuery.convertToDbQuery();
Bson combinedAsAnd = Filters.and(b1, b2);
collection.find(combinedAsAnd).projection(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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