有MySQL了,为什么还硬要搭个MongoDB集群?( 五 )


import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;import org.bson.Document;import org.junit.Test;
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;
public class MongoDBTest {
// 获取数据库连接对象 MongoDatabase mongoDatabase = MongoDBUtil.getNoIdentifyConnect("127.0.0.1", 27017, "test");
@Test public void insertOne() { Map<String, Object> map = new HashMap<String, Object>(); map.put("姓名", "哪吒编程"); map.put("性别", "男"); map.put("年龄", 18); MongoDBUtil.insertOne(map, mongoDatabase, "worker"); MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void insertMany() { Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("姓名", "哪吒编程2"); map1.put("性别", "男"); map1.put("年龄", 18); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("姓名", "妲己"); map2.put("性别", "女"); map2.put("年龄", 18); List<Map<String, Object>> listData = https://www.isolves.com/it/sjk/MYSQL/2023-05-24/new ArrayList<>(); listData.add(map1); listData.add(map2); MongoDBUtil.insertMany(listData, mongoDatabase, "worker"); MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void delectOne() { MongoDBUtil.delectOne("worker", "姓名", "妲己", mongoDatabase);
MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void deleteMany() {
MongoDBUtil.deleteMany("worker", "姓名", "哪吒编程", mongoDatabase); MongoDBUtil.deleteMany("worker", "姓名", "妲己", mongoDatabase);
MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void deleteAllDocument() { MongoDBUtil.deleteAllDocument("worker", mongoDatabase);
MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void deleteAllCollection() {
MongoDBUtil.deleteAllCollection("worker", mongoDatabase);
MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void updateOne() {
MongoDBUtil.updateOne("worker", "姓名", "哪吒编程2","姓名", "哪吒编程", mongoDatabase);
MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void updateMany() {
MongoDBUtil.updateMany("worker", "姓名", "哪吒编程2","姓名", "哪吒编程", mongoDatabase);
MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void find() { MongoCursor<Document> mongoCursor = MongoDBUtil.find("worker", mongoDatabase);
while (mongoCursor.hasNext()) { Document document = mongoCursor.next(); System.out.println(document + " size: " + document.size()); } MongoDBUtil.closeNoIdentifyConnect(); }
@Test public void filterfind() { MongoCursor<Document> mongoCursor = MongoDBUtil.Filterfind("worker", "姓名", "哪吒编程", mongoDatabase);
while (mongoCursor.hasNext()) { Document document = mongoCursor.next(); System.out.println(document + " size: " + document.size()); } MongoDBUtil.closeNoIdentifyConnect(); }}
作者丨哪吒
来源丨公众号:哪吒编程(ID:gh_61b183bcf690)

【有MySQL了,为什么还硬要搭个MongoDB集群?】


推荐阅读