1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@Document (collection= "Book" ) public class Book{ private String ISBN; private String bookTitle; private List< String > categories; //SETTERS - GETTERS } public class Category{ private String categoryId; private String categoryName; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
{ "isbn" : "11111111" , "bookTitle" : "My Book Title" , "categories" :[ { "categoryId" : "cat1" , "categoryName" : "My Category 1" }, { "categoryId" : "cat2" , "categoryName" : "My Category 2" } ] } { "isbn" : "22222222222" , "bookTitle" : "My Book Title2" , "categories" :[ { "categoryId" : "cat1" , "categoryName" : "My Category 1" }, { "categoryId" : "cat3" , "categoryName" : "My Category 3" } ] } { "isbn" : "333333333" , "bookTitle" : "My Book Title3" , "categories" :[ { "categoryId" : "cat2" , "categoryName" : "My Category 2" }, { "categoryId" : "cat3" , "categoryName" : "My Category 3" } ] } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
@Component public class MyDAO{ // Mongo template injected through Spring @Autowired private MongoOperations mongoTemplate; public void deleteCategories(String[] categories){ //find query // find all the programs containing the the categories passed as parameter Query findQuery = Query.query(Criteria.where( "categories.categoryId" ).in(Arrays.asList(categories))); // build the Update DBObject pullUpdate = BasicDBObjectBuilder.start().add( "categoryId" , BasicDBObjectBuilder.start() .add( "$in" , categories).get()).get(); Update update = new Update().pull( "categories" , pullUpdate ); // execute the update template.updateMulti(findQuery, update, Book. class ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public class SpringDataDemo{ public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:spring-beans.xml" ); MyDAO myDao = context.getBean( "myDao" ); //delete categories "cat1" and "cat2" mydao.deleteCategories( new String[]{ "cat1" , "cat2" }); } } |
What is broadcasts.broadcastId
ReplyDeleteIt's actually a typo from when I first started writing the article.
DeleteIt should be 'categories.categoryId'; its corrected now thanks for pointing it out.
Thanx dude for your post.
ReplyDeleteYou're welcome.
ReplyDeletegreat, example, any idea of how to update one field value in the array, I have tried that and could not get spring data to work- only a native query.
ReplyDelete// execute the update
ReplyDeletetemplate.updateMulti(findQuery, pullUpdate, Book.class);
should we use 'update' variable, instead of pullUpdate?
If not, why we introduce it?
Hi thanks for pointing this out, it's actually a typo.. you should use the update variable and not the pullUpdate (I've corrected it now).
DeleteAny idea to update an array field.
DeleteHi, what do you mean by update array field? you want to change the value of for example categoryName ?
DeleteIn java class you have a list of strings and in Mongo's document there is array of category objects. Should that be edited?
ReplyDeleteTks for share, I was looking for this code a long time
ReplyDelete