当前位置:Gxlcms >
数据库问题 >
不使用spring的情况下原生java代码两种方式操作mongodb数据库
不使用spring的情况下原生java代码两种方式操作mongodb数据库
时间:2021-07-01 10:21:17
帮助过:1人阅读
- 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;
-
- 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>();
-
- mongoCredentialList.add(MongoCredential.createMongoCRCredential(
- "admin", "admin", "123456".toCharArray()));
- MongoClient client = new MongoClient(sa, mongoCredentialList);
-
-
- DB mongoDB = client.getDB("mongoTest1");
- DBCollection collection1 = mongoDB.getCollection("userTest1");
-
- 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("****************************************************************************");
-
- MongoDatabase database = client.getDatabase("mongoTest2");
-
- 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数据库
标签: