Java操作MongoDB

运行环境:

java1.8
mongodb3.0
mongo-java-driver3.7.0


获取数据库

MongoClient client = MongoClients.create("mongodb://localhost:27017");
MongoDatabase db = client.getDatabase("test");

创建集合

MongoCollection<Document> mycol = db.createCollection("mycol");

获取集合

MongoCollection<Document> mycol = db.getCollection("mycol");

删除集合

db.getCollection("mycol").drop;
查看集合中所有文档
FindIterable<Document> iterator = db.getCollection("mycol").find();
	MongoCursor<Document> iterator = iterator.iterator();
	while (iterator.hasNext()) {
		System.out.println(iterator.next());
	}

获取集合中文档的个数

long count = db.getCollection("mycol").count();

插入一个文档

mycol.insertOne(new Document("title", 1234));

插入多个文档

Document doc1 = new Document("title", "title1");
Document doc2 = new Document("title", "title2");
List<Document> docs = new ArrayList<Document>();
docs.add(doc1);
docs.add(doc2);
mycol.insertMany(docs);

更新文档

Document doc = new Document("title", "title1");
Bson filter = Filters.eq("title", "title1");
Bson update = new Document("$set", new Document("title", "title2"));
mycol.updateMany(filter, update);

删除一个文档

Bson filter = Filters.eq("title", "title1");
mycol.deleteOne(filter);

删除多个文档

Bson filter = Filters.eq("title", "title2");
mycol.deleteMany(filter);

关闭连接

client.close();

准备测试mongo4.0事务,我在本地搭建了mongo服务并配置成副本集模式。当我使用springboot测试时能正常回滚,使用springmvc时有执行事务回滚代码,但是真实结果是没有进行事务回滚。 以下是我的代码片段: ``` ##springboot 2.1.6.RELEASE @Configuration public class MongoConfig extends AbstractMongoConfiguration { private static final Logger LOG = LoggerFactory.getLogger(MongoConfig.class); @Override public MongoClient mongoClient() { return new MongoClient("localhost", 27017); } @Override protected String getDatabaseName() { return "test"; } @Bean MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { LOG.info("mongo[{}]",dbFactory.getDb().getName()); return new MongoTransactionManager(dbFactory); } } //业务代码 @Transactional(rollbackFor = Exception.class) public void foo(){ mongoTemplate.insert(new Student("李四", "20", "####", "男")); int i = 1/0; } ``` 以上片段能够正常回滚。 ``` ##springmvc 5.1.8 <bean id="mongoClient" class="com.mongodb.MongoClient"> <constructor-arg name="host" value="${mongo.host}"/> <constructor-arg name="port" value="${mongo.port}"/> </bean> <bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"> <constructor-arg name="databaseName" value="${mongo.databaseName}"/> <constructor-arg name="mongoClient" ref="mongoClient"/> </bean> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongoClient"/> <constructor-arg name="databaseName" value="${mongo.databaseName}"/> </bean> <bean id="mongoTransactionManager" class="org.springframework.data.mongodb.MongoTransactionManager"> <constructor-arg name="dbFactory" ref="mongoDbFactory"/> </bean> <tx:annotation-driven transaction-manager="mongoTransactionManager" /> <tx:advice id="mongoAdvice" transaction-manager="mongoTransactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceMethod" expression="(@annotation(org.springframework.transaction.annotation.Transactional))"/> <aop:advisor pointcut-ref="serviceMethod" advice-ref="mongoAdvice"/> </aop:config> //业务代码 @Transactional(value = "mongoTransactionManager", rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) public void test() { mongoTemplate.insert(new Student("李四")); int i = 1 / 0; } ``` 以上代码执行了事务回滚代码,但是数据仍旧被插入。
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页