Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kam.albert.domain.test.case2;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.data.mongodb.core.MongoOperations;
- import org.springframework.data.mongodb.core.query.Criteria;
- import org.springframework.data.mongodb.core.query.Query;
- import org.springframework.data.mongodb.core.query.Update;
- import org.springframework.stereotype.Component;
- @Component
- public class Test {
- private static final String COLLECTION_NAME = "testing";
- @Autowired
- private MongoOperations ops;
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "test-context.xml"
- );
- Test app = ctx.getBean(Test.class);
- app.test();
- }
- public void test() {
- // create a post and add a comment
- String id = "pplId";
- People ppl = new People(id);
- // store it
- this.ops.insert(ppl, COLLECTION_NAME);
- Update update = new Update();
- // update = update.pushAll("names", new Object[] {"albert", "kam"});
- update = update.pushAll("persons", new Object[] {new Person("albert"), new Person("kam")});
- // update = update.push("persons", new Person("albert")).push("persons", new Person("kam"));
- // update = update.push("persons", new Person("albert"));
- this.ops.updateFirst(Query.query(Criteria.where("_id").is(id)), update, COLLECTION_NAME);
- }
- public static class People {
- private String id;
- private People() {}
- private People(String id) {
- this.id = id;
- }
- private List<String> names = new ArrayList<>();
- }
- public static class Person {
- private String id;
- public Person(String string) {
- this.id = string;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement