Spring Boot | Tutorial 28 : MongoTemplate with Aggregation, Projection and Dynamic APIs

preview_player
Показать описание
Learn Spring Boot from Scratch from Us.
Best Way to learn something is to create, create a project at the end and learn with us.

In this tutorial, we have discussed,

How to create CRUD with MongoTemplate and External Mongo
How to do aggregation with mongoTemplate
How to do projection with mongoTemplate
How to make dynamic APIs with paging functionality with mongoTemplate
How to search inside array in MongoDb with MongoTemplate.

Important Previous tutorials:
Spring Boot | Tutorial 21 : MongoClient and Embedded Mongo Part 1

Spring Boot | Tutorial 22 : MongoClient and Embedded Mongo Part 2

Spring Boot | Tutorial 23 : MongoClient and Embedded Mongo Part 3

Spring Boot | Tutorial 24 : MongoTemplate (MongoOperations, Query, Criteria) with Embedded Mongo

Spring Boot | Tutorial 25 : MongoRepository (Custom Queries) and Embedded Mongo

Spring Boot | Tutorial 27 : MongoClient with Aggregation, Projection, and Dynamic APIs

Mongo Operators:

In Upcoming Tutorial,
MongoRepository with Aggregation, Projection, and Dynamic APIs

Comment your questions and requests.
And Don’t Forget to Like, Share and Subscribe
Рекомендации по теме
Комментарии
Автор

Excellent - Thanks for the great presentation - Good Job

InterviewDOT
Автор

nice tutorial, How can I create aggregate query if I have reference document. I have stored the only reference object of other collection.I have to retrieve data from both collection mapped. what will be query for it.

Автор

I have a problem. I need to send the sub document from json object using range dates using conditions

perumalvaibhavam
Автор

Can you help me...I am searching json document on the basis of one attribute (city) so how to use @Query annotation with Case insensitive pattern so all data will appear where city is in capital letters or small...?

siddharthchoudhary
Автор

Will you please tell me, how to configure mongo template when I want to perform operations on multiple databases.
That means configuring mongo template for different connections.

anjalimishra
Автор

Sorry for bugging with some questions, I am trying out few Aggregation and when I am trying to use $addFields as one the Pipeline stage, I am stuck. Can you please review and help me with your suggestion.
This is my booksRepository code:
public Map<String, Object> categoryType) {
UnwindOperation
MatchOperation
GroupOperation
AddFieldsOperation addFieldOperation=AddFieldsOperation.addField("totalPages").withValueOfExpression("$sum", "$items.pageCount").build();
Aggregation aggregation = Aggregation.newAggregation(unwindOperation, matchOperation, groupByOperation, addFieldOperation);
return mongoTemplate.aggregate(aggregation, "books",
}


This is my basic aggregation pipeline expression:
[{$unwind: {
  path: "$categories",
  preserveNullAndEmptyArrays: false
}}, {$match: {
  "categories":{
    $eq:"Java"
  }
}}, {$group: {
  _id: "$status",
  items: {
    $push: "$$ROOT"
  }
}}, {$addFields: {
  totalPages: {
    $sum:"$items.pageCount"
  }
}}]

I am getting error as below while executing my code:
2020-07-04 17:05:01.064 ERROR 1350 --- [nio-9094-exec-2]   : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is Invalid reference '$sum'!] with root cause

Invalid reference '$sum'!
at
at

And if I am using like this: AddFieldsOperation
Then nothing seems to happen.

arkaimps
Автор

How can we search for nested data using Mongo template and Reactive mongo template? Could you please help.

vaibhavgoel
Автор

Great video. again..
Like me someone else also can face this issue, while using MongoTemplate with Local Mongo Instance and without @Document and specific Entity class, that it wasn't able to pull data from specific Collection, though mentioned collectionName as: mongoTemplate.findAll(Document.class, "books").
I had to another Key in my application.properties file as

arkaimps
Автор

Plz make a video to implement joins in mongodb

technoawaz
Автор

If my collection has name field in it.

But it has values like Suresh, suresh,
I want to perform case insensitive search.

How I can perform that.

viratsuresh
Автор

How to mock mongotemplate.aggregate..I'm getting null value

vigneshr
Автор

Hi Jayraj,
While running the below code:
Page<Document> pageDoc=PageableExecutionUtils.getPage(allDocuments, pageRequest, ()->mongoTemplate.count(query, Document.class));
I have getting below error:
Couldn't find PersistentEntity for type class org.bson.Document!
It seems like mapping issue. But I haven't created any entity class. Can you please suggest, what can be the issue?

arkaimps
Автор

Get more time to fetch Data in list use find method .

akshayjawale