Skip to main content

Execute Commands

Hint

This article is generated by AI translation.

Use JdbcTemplate to run MongoDB commands for read/write operations. Ensure the MongoDB datasource is configured; see the MongoDB driver guide.

Note

See JdbcTemplate for more patterns. The driver does not support:

  • Batching
  • Stored procedures

JdbcTemplate API

Define JdbcTemplate with a DataSource or Connection.

Create JdbcTemplate
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
// or
JdbcTemplate jdbc = new JdbcTemplate(connection);

Insert data

Insert data
// Direct command
jdbc.execute("test.user_info.insert({name: 'mali', age: 26})");
// Command with bind arguments
jdbc.execute("test.user_info.insert(?)", new Object[] { "{name: 'mali', age: 26}" });

Query data

List query
// All rows
List<Map<String, Object>> list = jdbc.queryForList("test.user_info.find()");
Conditional query
// Filter by condition
Map<String, Object> mali = jdbc.queryForMap("test.user_info.find({name: 'mali'})");
String json = (String) mali.get("JSON");

Update data

Update data
// Update age
jdbc.execute("test.user_info.update({name: 'mali'}, {$set: {age: 27}})");

Delete data

Delete data
// Delete matching records
jdbc.execute("test.user_info.remove({name: 'mali'})");