mongodbにおけるMongoTemplateの使用について
19323 ワード
データの取得
public void mongoDbJavaTest(){
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setName("admin");
mongoDbJavaTest.setRemarks(" ");
Query query = Query.query(Criteria.where("name").is("admin"));
mongoTemplate.find(query,MongoDbJavaTest.class);
mongoTemplate.find(query,MongoDbJavaTest.class,"mongodb_java_test");
mongoTemplate.findAll(MongoDbJavaTest.class);
mongoTemplate.findAll(MongoDbJavaTest.class,"mongodb_java_test");
mongoTemplate.query(MongoDbJavaTest.class);
}
データの挿入
public void mongoDbJavaSave(){
List<MongoDbJavaTest> list = new ArrayList<>();
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setName("admin");
mongoDbJavaTest.setRemarks(" ");
list.add(mongoDbJavaTest);
// mongodb
mongoTemplate.save(mongoDbJavaTest);
mongoTemplate.insert(mongoDbJavaTest);
// mongodb
mongoTemplate.save(mongoDbJavaTest,"mongodb_java_test");
mongoTemplate.insert(mongoDbJavaTest,"mongodb_java_test");
// list mongodb
mongoTemplate.save(list,"mongodb_java_test");
mongoTemplate.insert(list,"mongodb_java_test");
mongoTemplate.insert(list,MongoDbJavaTest.class);
}
データの更新
public void mongoDbJavaUpdate(){
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setId("5d1312aeb1829c279c6c256b");
mongoDbJavaTest.setName("admin");
mongoDbJavaTest.setRemarks(" ");
Query query = Query.query(Criteria.where("_id").is("5d1312aeb1829c279c6c256b"));
Update update = Update.update("name","admin");
//
mongoTemplate.updateFirst(query,update, MongoDbJavaTest.class);
mongoTemplate.updateFirst(query,update, "mongodb_java_test");
mongoTemplate.updateFirst(query,update, MongoDbJavaTest.class,"mongodb_java_test");
//
mongoTemplate.update(mongoDbJavaTest.getClass());
//
mongoTemplate.updateMulti(query,update, MongoDbJavaTest.class);
mongoTemplate.updateMulti(query,update,"mongodb_java_test");
mongoTemplate.updateMulti(query,update, MongoDbJavaTest.class,"mongodb_java_test");
// ,
mongoTemplate.upsert(query,update, MongoDbJavaTest.class);
mongoTemplate.upsert(query,update,"mongodb_java_test");
mongoTemplate.upsert(query,update, MongoDbJavaTest.class,"mongodb_java_test");
}
データの削除
public void mongoDbJavaRemove(){
List<MongoDbJavaTest> list = new ArrayList<>();
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setId("5d1312aeb1829c279c6c256b");
list.add(mongoDbJavaTest);
Query query = Query.query(Criteria.where("_id").in("5d1312aeb1829c279c6c256b","5d13133ab1829c29d02ce29c"));
//
mongoTemplate.remove(query);
mongoTemplate.remove(mongoDbJavaTest);
mongoTemplate.remove(MongoDbJavaTest.class);
// ( )
mongoTemplate.remove(query,CarryUserInfo.class);
mongoTemplate.remove(query,MongoDbJavaTest.class,"mongodb_java_test");
}
詳細:MongoTemlate