Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized queries with Java and MongoDB

Tags:

java

mongodb

api

Can you do parameterized queries with Java and MongoDB - kind of like prepared statements with JDBC?

What I'd like to do is something like this. Set up a query that takes a date range - and then call it with different ranges. I understand that DBCursor.find(...) doesn't work this way - this is kind of pseudo-code to illustrate what I'm looking for.

DBCollection dbc = ...
DBObject pQuery = (DBObject) JSON.parse("{'date' : {'$gte' : ?}, 'date' : {'$lte' : ?}}");
DBCursor aprilResults = dbc.find(pQuery, "2012-04-01", "2012-04-30");
DBCursor mayResults = dbc.find(pQuery, "2012-05-01", "2012-05-31");
...
like image 904
Mark Hansen Avatar asked May 18 '12 02:05

Mark Hansen


People also ask

How write parameterized SQL query in Java?

Create your first Named Parameterized Query in a minute String sqlQuery = " INSERT INTO employee (id, name, designation, salary)" + " VALUES(:id, :name, :designation, :salary)"; PreparedStatement prepStmt = NamedPreparedStatement. prepareStatement(connection, sqlQuery); prepStmt. setLong("id", 1); prepStmt.

How do I run a MongoDB query in Robo 3T?

Open Studio 3T and connect to your MongoDB database. Next, open the Import Wizard from the toolbar. Then, choose JSON as the import format. Click OK.


1 Answers

MongoDB itself doesn't support anything like this, but then again, it doesn't take too much sense as it needs to send the query over to the server every time anyway. You can simply construct the object in your application yourself, and just modify specific parts by updating the correct array elements.

like image 80
Derick Avatar answered Oct 07 '22 00:10

Derick