java操作mongodb基础
第一步:安装软件(docker)
windows安装软件(msi)略
第二步:引入jar包、java测试
package mongodb.testMongodb;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.client.FindIterable;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
public class Ttest1 {
public static void main(String[] args) {
// 客户端创建连接
MongoClient client = new MongoClient("localhost", 27017);
// 数据库
MongoDatabase studentdb = client.getDatabase("student");
// 获取集合(表)
MongoCollection<Document> student = studentdb.getCollection("student");
// 获取迭代器
FindIterable<Document> documents = student.find();
System.out.println(documents);
for (Document document : documents) {
System.out.println(document.getString("content"));
}
System.out.println("=========");
// 创建集合
// studentdb.createCollection("testCreateCollection");
// 插入文档
/**
* 1. 创建文档 org.bson.Document 参数为key-value的格式 2. 创建文档集合List<Document> 3.
* 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用
* mongoCollection.insertOne(Document)
*/
Document document = new Document("title", "mongodb").append("describe", "databse").append("like", 100).append(
"by", "fiy");
List<Document> docsList = new ArrayList<Document>();
docsList.add(document);
student.insertMany(docsList);
System.out.println("文档插入成功");
System.out.println("=========");
// 检索所有文档
/**
* 1. 获取迭代器FindIterable<Document> 2. 获取游标MongoCursor<Document> 3.
* 通过游标遍历检索出的文档集合
*/
FindIterable<Document> findIterable = student.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while (mongoCursor.hasNext()) {
System.out.println(mongoCursor.next());
}
System.out.println("=========");
// 更新文档
student.updateMany(Filters.eq("like", 100), new Document(("$set"), new Document("like", 200)));
FindIterable<Document> findIterable1 = student.find();
MongoCursor<Document> mongoCursor1 = findIterable1.iterator();
while (mongoCursor1.hasNext()) {
System.out.println(mongoCursor1.next());
}
System.out.println("=========");
// 删除符合条件的第一个文档
// student.deleteOne(Filters.eq("like", 200));
// 删除所有符合条件的文档
// student.deleteMany (Filters.eq("like", 200));
// 查询bson
BasicDBObject bson = new BasicDBObject("like", 200);
FindIterable<Document> docsFindIterable = student.find(bson);
MongoCursor<Document> cursor = docsFindIterable.iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
System.out.println("=========");
// 查询一个
System.out.println(/* Document doc= */student.find(bson).first());
System.out.println("=========");
// map
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("like", 200);
BasicDBObject bson1 = new BasicDBObject(map);
FindIterable<Document> docsFindIterable1 = student.find(bson);
MongoCursor<Document> cursor2 = docsFindIterable1.iterator();
while (cursor2.hasNext()) {
System.out.println(cursor2.next());
}
System.out.println("=========");
// Bson
Bson bson2 = Filters.eq("like", 200);
FindIterable<Document> docsFindIterable2 = student.find(bson2);
MongoCursor<Document> cursor3 = docsFindIterable2.iterator();
while (cursor3.hasNext()) {
System.out.println(cursor3.next());
}
System.out.println("=========");
client.close();
}
}
输出:
五月 04, 2020 11:14:36 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
com.mongodb.client.internal.Java8FindIterableImpl@4fe3c938
五月 04, 2020 11:14:36 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: Cluster description not yet available. Waiting for 30000 ms before timing out
五月 04, 2020 11:14:36 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: Opened connection [connectionId{localValue:1, serverValue:50}] to localhost:27017
五月 04, 2020 11:14:36 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 6]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2520200}
五月 04, 2020 11:14:36 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: Opened connection [connectionId{localValue:2, serverValue:51}] to localhost:27017
坚持就是胜利
加班加到深夜
坚持就是胜利
明天更美好
加班加到深夜
null
null
null
null
null
null
=========
文档插入成功
=========
Document{{_id=4, content=坚持就是胜利, userid=1014, name=tiger, visits=1234}}
Document{{_id=5, content=加班加到深夜, userid=1035, name=rock, visits=7254}}
Document{{_id=1, content=坚持就是胜利, userid=136, name=tim, visits=5656}}
Document{{_id=7, content=明天更美好, userid=178, name=alise, visits=776}}
Document{{_id=2, content=加班加到深夜, userid=1035, name=rock, visits=7254}}
Document{{_id=5eaf74e8ab141d3a36d48741, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf77e826fd8c214455731f, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf78b7133ab634b8e8569a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf798b313aeb6aa863098a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7a5372f1bf1a9df0f7ad, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7bb56664fb7b5e4c20fe, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf889c8651b7731376f1ae, title=mongodb, describe=databse, like=100, by=fiy}}
=========
Document{{_id=4, content=坚持就是胜利, userid=1014, name=tiger, visits=1234}}
Document{{_id=5, content=加班加到深夜, userid=1035, name=rock, visits=7254}}
Document{{_id=1, content=坚持就是胜利, userid=136, name=tim, visits=5656}}
Document{{_id=7, content=明天更美好, userid=178, name=alise, visits=776}}
Document{{_id=2, content=加班加到深夜, userid=1035, name=rock, visits=7254}}
Document{{_id=5eaf74e8ab141d3a36d48741, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf77e826fd8c214455731f, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf78b7133ab634b8e8569a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf798b313aeb6aa863098a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7a5372f1bf1a9df0f7ad, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7bb56664fb7b5e4c20fe, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf889c8651b7731376f1ae, title=mongodb, describe=databse, like=200, by=fiy}}
=========
Document{{_id=5eaf74e8ab141d3a36d48741, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf77e826fd8c214455731f, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf78b7133ab634b8e8569a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf798b313aeb6aa863098a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7a5372f1bf1a9df0f7ad, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7bb56664fb7b5e4c20fe, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf889c8651b7731376f1ae, title=mongodb, describe=databse, like=200, by=fiy}}
=========
Document{{_id=5eaf74e8ab141d3a36d48741, title=mongodb, describe=databse, like=200, by=fiy}}
=========
Document{{_id=5eaf74e8ab141d3a36d48741, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf77e826fd8c214455731f, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf78b7133ab634b8e8569a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf798b313aeb6aa863098a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7a5372f1bf1a9df0f7ad, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7bb56664fb7b5e4c20fe, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf889c8651b7731376f1ae, title=mongodb, describe=databse, like=200, by=fiy}}
=========
Document{{_id=5eaf74e8ab141d3a36d48741, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf77e826fd8c214455731f, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf78b7133ab634b8e8569a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf798b313aeb6aa863098a, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7a5372f1bf1a9df0f7ad, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf7bb56664fb7b5e4c20fe, title=mongodb, describe=databse, like=200, by=fiy}}
Document{{_id=5eaf889c8651b7731376f1ae, title=mongodb, describe=databse, like=200, by=fiy}}
=========
五月 04, 2020 11:14:36 上午 com.mongodb.diagnostics.logging.JULLogger log
信息: Closed connection [connectionId{localValue:2, serverValue:51}] to localhost:27017 because the pool has been closed.