How to initialize MongoClient once in Spring Boot and use its methods?

Here are couple of ways of creating an instance of MongoClient, configuring and using it within the Spring Boot application. (1) Registering a Mongo Instance using Java-based Metadata: @Configuration public class AppConfig { public @Bean MongoClient mongoClient() { return MongoClients.create(); } } Usage from CommandLineRunner‘s run method (all examples are run similarly): @Autowired MongoClient mongoClient; … Read more

Spring data mongodb – The ‘cursor’ option is required

MongoDB changed in 3.6 how the aggregation command works. Aggregations require now a cursor. We adapted Spring Data MongoDB 2.1 but not previous versions. Aggregations must be invoked through the collection’s aggregate(…) method instead of calling the command directly. This is also the reason why we didn’t backport the change. executeCommand(…) is no longer called … Read more

How to do a Mongo aggregation query in Spring Data?

Although this is old thread, but I hope whoever found this thread can now safely for doing multi stage/pipeline aggregation(not quite sure what it’s call) in MongoRepository. As I’m also struggling looking for clue and example of aggregation in mongo repository without mongo template. But now, I’m able to do the Aggregation pipeline as per … Read more

Mongo how to $lookup with DBRef

Actually, the other answer is wrong. It is possible to do a lookup on a DBref field within your aggregator, and you don’t need mapreduce for that. Solution db.A.aggregate([ { $project: { B_fk: { $map: { input: { $map: { input:”$bid”, in: { $arrayElemAt: [{$objectToArray: “$$this”}, 1] }, } }, in: “$$this.v”}}, } }, { … Read more

MongoDB-CR Authentication failed

go to mongoDB console and delete your current user & set authSchema version to 3 instead of 5 , follow these commands in mongo console – mongo use admin db.system.users.remove({}) <== removing all users db.system.version.remove({}) <== removing current version db.system.version.insert({ “_id” : “authSchema”, “currentVersion” : 3 }) Now restart the mongod and create new user … Read more

MongoDB Full and Partial Text Search

As at MongoDB 3.4, the text search feature is designed to support case-insensitive searches on text content with language-specific rules for stopwords and stemming. Stemming rules for supported languages are based on standard algorithms which generally handle common verbs and nouns but are unaware of proper nouns. There is no explicit support for partial or … Read more

What’s the difference between Spring Data’s MongoTemplate and MongoRepository?

“Convenient” and “powerful to use” are contradicting goals to some degree. Repositories are by far more convenient than templates but the latter of course give you more fine-grained control over what to execute. As the repository programming model is available for multiple Spring Data modules, you’ll find more in-depth documentation for it in the general … Read more