How to use low-level driver APIs using Spring Data MongoDB

I am using Spring Data MongoDB. But I do not want to match my result to a domain class. In addition, I want to access the low-level MongoAB APIs in several cases. But I want Spring to manage connection pools, etc.

How can I get an instance of com.mongodb.MongoClient to perform low level operations. Here is what I am trying to do:

 MongoClient mongoClient = new MongoClient(); DB local = mongoClient.getDB("local"); DBCollection oplog = local.getCollection("oplog.$main"); DBCursor lastCursor = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1); 

Or I just want a JSON / DBCursor / DBObject object.

+4
source share
2 answers

you can do it this way

 @Autowired MongoDbFactory factory; DB local = factory.getDB("local"); DBCollection oplog = local.getCollection("oplog.$main"); DBCursor lastCursor = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1); 

Where

 MongoDbFactory is an interface provifed by spring-data-mongo that can obtain a com.mongodb.DB object and access allthe functionality of a specific MongoDB database instance 

Your configuration file should contain the following data:

 <bean id="mongoFactoryBean" class="org.springframework.data.mongodb.core.MongoFactoryBean"> <property name="host" value="127.0.0.1"/> <property name="port" value="27017"/> </bean> <bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"> <constructor-arg name="mongo" ref="mongoFactoryBean"/> <constructor-arg name="databaseName" value="local"/> </bean> 

doing this, spring should remain the connection pool manager.

+2
source

Typically, you perform low-level access using the MongoTemplate execute(…) methods, which accept callbacks giving you access to the Mongo source API.

  class MyClient { private final MongoOperations operations; @Autowired public MyClient(MongoOperations mongoOperations) { this.operations = operations; } void yourMethod() { operations.execute(new CollectionCallback<YourDomainClass>() { YourDomainClass doInCollection(DBCollection collection) { // here goes your low-level code } }); } 

The advantage of this template approach is that a MongoTemplate instance that supports the MongoOperations interface will still take care of all resource management and exception DataAccessException (converting all Mongo specific exceptions to the Spring DataAccessException hierarchy).

However, for your specific example, you can simply go straight ahead and do the following:

  Query query = new Query().with(new Sort(DESC, "$natural")).limit(1); DBObject result = operations.find(query, DBObject.class, "oplog.$main"); 

Here you can mix and match the type that you pass to the find(…) method so that the template can transform the result into a Map object or domain object, if necessary. As stated above, you also get resource management and translation of exceptions that are not in your sample code.

+2
source

Source: https://habr.com/ru/post/1501158/


All Articles