importjava.util.ArrayList;importjava.util.List;importjava.util.regex.Pattern;importorg.bson.Document;importcom.mongodb.MongoClient;importcom.mongodb.MongoCredential;importcom.mongodb.ServerAddress;importcom.mongodb.client.FindIterable;importcom.mongodb.client.MongoCollection;importcom.mongodb.client.MongoCursor;importcom.mongodb.client.MongoDatabase;public classMongoZyh {public static voidmain(String[] args) {try{//连接到MongoDB服务,ServerAddress()两个参数分别为 服务器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
List addrs = new ArrayList();
addrs.add(serverAddress);//三个参数分别为 用户名 数据库名称 密码
MongoCredential credential =MongoCredential
.createScramSha1Credential("zyh", "admin","zyh".toCharArray());
List credentials = new ArrayList();
credentials.add(credential);//通过连接认证获取MongoDB连接
MongoClient mongoClient = newMongoClient(addrs, credentials);//连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("zyhdb");//新建集合,执行后会在数据库里新建一个空的集合//mongoDatabase.createCollection("student");//System.out.println("新建集合成功");//获取集合,并往集合中插入数据
MongoCollection mongoCollection =mongoDatabase
.getCollection("student");//插入一条数据//Document document = new Document();//document.append("name", "zhangsan");//document.append("age", 28);//mongoCollection.insertOne(document);//System.out.println("插入一条数据成功");//插入多条数据//List documentList = new ArrayList();//Document document1 = new Document();//document1.append("name", "lisi");//document1.append("age", 28);//document1.append("sex", "男");//Document document2 = new Document();//document2.append("name", "wangwu");//document2.append("age", 31);//document2.append("sex", "男");//documentList.add(document1);//documentList.add(document2);//mongoCollection.insertMany(documentList);//System.out.println("插入多条数据成功");//查询数据//查询集合中所有的数据//FindIterable findIterable = mongoCollection.find();//MongoCursor mongoCursor = findIterable.iterator();//while (mongoCursor.hasNext()) {//System.out.println(mongoCursor.next());//}//根据条件查询//Document query = new Document();//query.put("age", new Document("$lt", 30));//query.put("sex", "男");//query.put("name", query);//正则表达式查询//Pattern pattern = Pattern.compile("^zhang");//query.put("name", pattern);//排序//Document sort = new Document();//sort.put("name", -1);//1是正序,-1是倒序//FindIterable findIterable = mongoCollection.find(query)//.sort(sort);//MongoCursor mongoCursor = findIterable.iterator();//while (mongoCursor.hasNext()) {//Document doc = mongoCursor.next();//System.out.print("name:" + doc.get("name") + "...");//System.out.print("age:" + doc.get("age") + "...");//System.out.println("sex:" + doc.get("sex") + "...");//}//mongoCollection.findOneAndUpdate(查询条件, 修改内容);//查询出第一条数据并修改//mongoCollection.findOneAndDelete(查询条件);//查询出第一条数据并删除//mongoCollection.findOneAndReplace(查询条件, 替换内容);//查询出第一条数据并替换//修改数据//Document query = new Document();//query.put("age", 28);//Document update = new Document();//Document d = new Document();//d.put("birthday", new Date());//d.put("name", "zhangsan");//update.put("$set", d);//mongoCollection.updateOne(query, update);//修改查询到的第一条数据//mongoCollection.updateMany(查询条件, 修改内容);//修改查询到的所有数据//删除数据//Document query = new Document();//query.put("age", 28);//mongoCollection.deleteOne(query);//删除查询到的第一条数据//mongoCollection.deleteMany(查询条件);//删除查询到的所有数据//mongoCollection.drop();//删除集合
}catch(Exception e) {
e.printStackTrace();
}
}
}