Combine two arraialists in a simple form of Polish notation

I have two arraylists of type String, one of the operands and one of the operators

ArrayList<String> operands = new ArrayList<String>(); ArrayList<String> operators = new ArrayList<String>(); 

They are filled so

 operands = { "\"symbol\": \"CHKP%\"", "\"price\": {$gt: 23.72\" }; operators = { "and"}; 

Ideally, I would convert this to a single ArrayList array that populates like this

 ArrayList<String> polishNotation = { "and", "\"symbol\": \"CHKP%\"", "\"price\": {$gt: 23.72\" }; 

It would be easy to rigidly define the Polish notation for three elements, but I have a different number of operators and operands (up to four operands and three operators). This code should be used to convert SQL select statements to MongoDB.find () statements. Any pointers on how to implement the ArrayList merge into Polish notation (prefix plate notation) would be greatly appreciated.

[Edit 2] The following is an example of an SQL statement with three statements ("like", "and", "<") and three operands ("FLIR%", "price", "price") and the MongoDB equivalent. I think using Polish notation can help me convert a SQL query query into a query programmed by Mongo

in SQL

 SELECT * FROM STOCK WHERE symbol like 'FLIR%' and price > 24.04 and price < 24.39; 

in MongoDB

 db.STOCK.find({ "symbol": "FLIR%", "price": { "$gt": 24.04, "$lt": 24.39 } } 
+7
java arraylist mongodb polish-notation
source share
1 answer

If you are planning to write such a parser, this will be a rather large project, because the sql query can become more and more complex. You can try using ANTLR . It has sql grammar. Or GeneralSqlParser or another parser to tokenize your sql statement, and then create your mongo statements.

If you're not particularly into writing a program, you can rely on the Query Mongo project . He does what you need. Please refer to this site.

However, you decide to get a quick solution for this through Java, you can try the program below. This uses jsoup to send a request to the querymongo site and receive the mongo request.

Hope this helps :)

 import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class MongoQuery { public static void main(String[] args) throws Exception { System.setProperty("http.proxyHost", "10.9.248.37"); System.setProperty("http.proxyPort", "18082"); Document doc = Jsoup.connect("http://www.querymongo.com/") .data("MySQLQuery", "SELECT * FROM STOCK WHERE symbol like 'FLIR%' and price > 24.04 and price < 24.39") .post(); for(Element e : doc.select("#mongoQuery")){ System.out.println(e.val()); } } } 
+4
source share

All Articles