Advertisement
Guest User

Untitled

a guest
Mar 16th, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package kam.albert.domain.test.case2;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import org.springframework.data.mongodb.core.MongoOperations;
  10. import org.springframework.data.mongodb.core.query.Criteria;
  11. import org.springframework.data.mongodb.core.query.Query;
  12. import org.springframework.data.mongodb.core.query.Update;
  13. import org.springframework.stereotype.Component;
  14.  
  15. @Component
  16. public class Test {
  17.  
  18.     private static final String COLLECTION_NAME = "testing";
  19.  
  20.     @Autowired
  21.     private MongoOperations ops;
  22.  
  23.     /**
  24.      * @param args
  25.      */
  26.     public static void main(String[] args) {
  27.         ApplicationContext ctx = new ClassPathXmlApplicationContext(
  28.             "test-context.xml"
  29.         );
  30.         Test app = ctx.getBean(Test.class);
  31.         app.test();
  32.     }
  33.  
  34.     public void test() {
  35.         // create a post and add a comment
  36.         String id = "pplId";
  37.         People ppl = new People(id);
  38.  
  39.         // store it
  40.         this.ops.insert(ppl, COLLECTION_NAME);
  41.  
  42.         Update update = new Update();
  43. //      update = update.pushAll("names", new Object[] {"albert", "kam"});
  44.         update = update.pushAll("persons", new Object[] {new Person("albert"), new Person("kam")});
  45. //      update = update.push("persons", new Person("albert")).push("persons", new Person("kam"));
  46. //      update = update.push("persons", new Person("albert"));
  47.         this.ops.updateFirst(Query.query(Criteria.where("_id").is(id)), update, COLLECTION_NAME);
  48.     }
  49.  
  50.     public static class People {
  51.  
  52.         private String id;
  53.         private People() {}
  54.         private People(String id) {
  55.             this.id = id;
  56.         }
  57.         private List<String> names = new ArrayList<>();
  58.     }
  59.  
  60.     public static class Person {
  61.         private String id;
  62.  
  63.         public Person(String string) {
  64.             this.id = string;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement