java 怎么封装操作mongodb的 baseDao 不用spring data mongodb
1个回答
展开全部
MongoClient client = new MongoClient("192.168.0.110", 27017);
DBCollection dbCollection = client.getDB("test").getCollection("update_array");
dbCollection.drop();
dbCollection = client.getDB("test").getCollection("update_array");
BasicDBObject dbObject = new BasicDBObject();
dbObject.append("id", 583739819102582565L).append("kids", Arrays.asList("1", "2", "3"));
dbCollection.insert(dbObject);
System.out.println(dbCollection.findOne());
BasicDBObject dbObject2 = new BasicDBObject(dbObject);
dbObject2.append("kids", Arrays.asList("1", "2", "3", "4"));
dbCollection.update(dbObject, dbObject2);
System.out.println(dbCollection.findOne());
测试结果:
{ "_id" : { "$oid" : "546f37f7d01de241a1e143e8"} , "id" : 583739819102582565 , "kids" : [ "1" , "2" , "3"]}
{ "_id" : { "$oid" : "546f37f7d01de241a1e143e8"} , "id" : 583739819102582565 , "kids" : [ "1" , "2" , "3" , "4"]}
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.3</version>
</dependency>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询