Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert document to specific collection in mongodb using spring data?

By default save() is used to add a document into the collection (defined in Model class). How to insert a document or Query to insert a document into the specific collection using Spring Data? Something like save(document,CollectionName).

like image 982
Arvind26 Avatar asked Sep 11 '25 09:09

Arvind26


1 Answers

The MongoOperations and MongoTemplate classes have an option in the save method to pass the collection name.

The class org.springframework.data.repository.CrudRepository is present in the core library which is not specific to Mongo. So, it doesn't have an option to pass the Mongo collection name.

MongoOperations save with collection name

MongoTemplate save with collection name

like image 101
notionquest Avatar answered Sep 12 '25 22:09

notionquest