java 怎么封装操作mongodb的 baseDao 不用spring data mongodb
发布网友
发布时间:2022-04-30 23:16
我来回答
共2个回答
懂视网
时间:2022-05-01 03:37
- package monAndMysql;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Set;
- import org.bson.BsonDocument;
- import org.bson.BsonString;
- import org.bson.Document;
- import org.bson.conversions.Bson;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.MongoClient;
- import com.mongodb.MongoCredential;
- import com.mongodb.ServerAddress;
- import com.mongodb.client.FindIterable;
- import com.mongodb.client.MongoCollection;
- import com.mongodb.client.MongoCursor;
- import com.mongodb.client.MongoDatabase;
- import com.mongodb.client.model.Filters;
-
- /**
- * mongodb和mysql性能测试
- *
- * @author tuzongxun123
- *
- */
- public class MonAndMysqlTest {
-
- public static void main(String[] args) {
- mongodbTest();
- }
-
- public static void mongodbTest() {
- ServerAddress sa = new ServerAddress("192.168.0.7", 27017);
- List<MongoCredential> mongoCredentialList = newArrayList<MongoCredential>();
- // java代码连接mongodb3.0数据库验证,userName,dbName,password
- mongoCredentialList.add(MongoCredential.createMongoCRCredential(
- "admin", "admin", "123456".toCharArray()));
- MongoClient client = new MongoClient(sa, mongoCredentialList);
- // 第一种方式
- // 第一种方式获取db,该方法已经不建议使用
- DB mongoDB = client.getDB("mongoTest1");
- DBCollection collection1 = mongoDB.getCollection("userTest1");
- // 这里的数据类型是dbobject
- DBObject document1 = new BasicDBObject();
- document1.put("name", "mongoTest1");
- document1.put("createTime", new Date().getTime());
- // 插入数据
- collection1.insert(document1);
- // 查询数据
- DBCursor cursor1 = collection1.find();
- System.out.println("第一种方式插入数据的结果:");
- while (cursor1.hasNext()) {
- DBObject object = cursor1.next();
- Set<String> keySet = object.keySet();
- for (String key : keySet) {
- System.out.println(key + ":" + object.get(key));
- }
- }
- // 更改数据
- DBObject query = new BasicDBObject();
- DBObject update = new BasicDBObject();
- query.put("name", "mongoTest1");
- update.put("$set", new BasicDBObject("name", "update1"));
- collection1.update(query, update);
- System.out
- .println("--------------------------------------------------------------------------------------");
- System.out.println("第一种方式修改数据的结果:");
- DBCursor cursor11 = collection1.find();
- while (cursor11.hasNext()) {
- DBObject object = cursor11.next();
- Set<String> keySet = object.keySet();
- for (String key : keySet) {
- System.out.println(key + ":" + object.get(key));
- }
- }
- // 删除数据
- DBObject query1 = new BasicDBObject();
- query1.put("name", "update1");
- collection1.remove(query1);
- System.out
- .println("--------------------------------------------------------------------------------------");
- System.out.println("第一种方式删除数据的结果:");
- DBCursor cursor12 = collection1.find();
- while (cursor12.hasNext()) {
- DBObject object = cursor12.next();
- Set<String> keySet = object.keySet();
- for (String key : keySet) {
- System.out.println(key + ":" + object.get(key));
- }
- }
- // 第二种方式
- System.out
- .println("****************************************************************************");
- // 第二种方式获取db及插入数据和查询操作。推荐方式
- MongoDatabase database = client.getDatabase("mongoTest2");
- // 注意这里的数据类型是document
- Document document2 = new Document();
- document2.put("name", "mongoTest2");
- document2.put("createTime", new Date().getTime());
- MongoCollection collection2 = database.getCollection("userTest2");
- // 插入数据
- collection2.insertOne(document2);
- // 查询数据,注意这里直接查询出的结果不是游标,还需要转换
- FindIterable<Document> findIterable = collection2.find();
- MongoCursor<Document> cursor2 = findIterable.iterator();
- System.out.println("第二种方式插入数据的结果:");
- while (cursor2.hasNext()) {
- Document document = cursor2.next();
- Set<String> keySet = document.keySet();
- for (String key : keySet) {
- System.out.println(key + ":" + document.get(key));
- }
- }
- // 更改数据
-
- Bson filter = Filters.eq("name", "mongoTest2");
- BsonDocument update2 = new BsonDocument();
- update2.put("$set", new BsonDocument("name", new BsonString("update2")));
- collection2.updateOne(filter, update2);
- System.out
- .println("--------------------------------------------------------------------------------------");
- MongoCursor<Document> cursor21 = findIterable.iterator();
- System.out.println("第二种方式更改数据的结果:");
- while (cursor21.hasNext()) {
- Document document = cursor21.next();
- Set<String> keySet = document.keySet();
- for (String key : keySet) {
- System.out.println(key + ":" + document.get(key));
- }
- }
- // 删除数据
- Bson filter2 = Filters.eq("name", "update2");
- collection2.deleteOne(filter2);
- System.out
- .println("--------------------------------------------------------------------------------------");
- MongoCursor<Document> cursor22 = findIterable.iterator();
- System.out.println("第二种方式删除数据的结果:");
- while (cursor22.hasNext()) {
- Document document = cursor22.next();
- Set<String> keySet = document.keySet();
- for (String key : keySet) {
- System.out.println(key + ":" + document.get(key));
- }
- }
- // 关闭数据库连接
- client.close();
- }
-
- }
执行main方法后,控制台打印结果如下图所示,证明操作都是没有问题的:
不使用spring的情况下原生java代码两种方式操作mongodb数据库
标签:
热心网友
时间:2022-05-01 00:45
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>