Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update Object with Spring Data and MongoDB?

How do I update Object with Spring Data and MongoDB?

do I just do a template.save()?

  public Person update( String id, String Name ) 
    {
        logger.debug("Retrieving an existing person");
        // Find an entry where pid matches the id

        Query query = new Query(where("pid").is(id));
        // Execute the query and find one matching entry
        Person person = mongoTemplate.findOne("mycollection", query, Person.class);

        person.setName(name);
        /**
        * How do I update the database
        */

        return person;
    }
like image 487
user2428795 Avatar asked Jul 03 '13 12:07

user2428795


People also ask

Can we use Spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.

Which annotation is used to link a MongoDB document with a spring bean?

To take full advantage of the object mapping functionality inside the Spring Data/MongoDB support, you should annotate your mapped objects with the @org. springframework. data. mongodb.


1 Answers

If you read the javadoc for MongoOperations/MongoTemplate you will see that

save()

performs an:

upsert() 

So yes you can just update your object and call save.

like image 200
Trevor Gowing Avatar answered Sep 28 '22 12:09

Trevor Gowing