Example usage for org.springframework.data.mongodb.core MongoOperations save

List of usage examples for org.springframework.data.mongodb.core MongoOperations save

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoOperations save.

Prototype

<T> T save(T objectToSave);

Source Link

Document

Save the object to the collection for the entity type of the object to save.

Usage

From source file:ezbake.example.ezmongo.EzMongoSpringDataSampleClient.java

public static void main(String[] args)
        throws VisibilityParseException, ClassificationConversionException, EzMongoBaseException {

    // For XML/*  w  w w. j  a  va 2 s.c om*/
    ApplicationContext ctx = new GenericXmlApplicationContext("springDataConfig.xml");

    // For Annotation
    //ApplicationContext ctx =
    //        new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    User user = new User("mkyong", "password123");

    // Here, we would insert the security tagging fields into the DBObject by calling a utility class (RedactHelper.java).
    Visibility vis = new Visibility();
    // Convert CAPCO to Accumulo-style boolean expression string and set it in the Visibility object.
    String booleanExpressionString = ClassificationUtils.getAccumuloVisibilityStringFromCAPCO("SECRET");
    vis.setFormalVisibility(booleanExpressionString);
    RedactHelper.setSecurityFieldsInDBObject(user, vis, "testAppId");

    // Also set the Name field in the User
    Name name = new Name("testFirstName", "testLastName");
    Visibility nameVis = new Visibility();
    nameVis.setFormalVisibility(booleanExpressionString);
    RedactHelper.setSecurityFieldsInDBObject(name, nameVis, "testAppId");

    user.setName(name);

    // Call the Provenance service to get a unique ID for the document -
    //   the unique ID would be used for the Purge feature.

    // save
    mongoOperation.save(user);

    // now user object got the created id.
    System.out.println("1. user : " + user);

    // query to search user
    Query searchUserQuery = new Query(Criteria.where("username").is("mkyong"));

    // find the saved user again.
    User savedUser = mongoOperation.findOne(searchUserQuery, User.class);
    System.out.println("2. find - savedUser : " + savedUser);

}

From source file:example.springdata.mongodb.util.BlogPostInitializer.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void loadFromClasspathSource(MongoOperations operations) throws Exception {

    Jackson2ResourceReader reader = new Jackson2ResourceReader();

    Object source = reader.readFrom(new ClassPathResource("spring-blog.atom.json"),
            this.getClass().getClassLoader());

    if (source instanceof Iterable) {
        ((Iterable) source).forEach(element -> operations.save(element));
    } else {/*w w w  . j  av a2  s.c om*/
        operations.save(source);
    }

    log.info("Imported blog posts from classpath!");
}