MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解
MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为10个)。
下面代码给大家介绍MongoDB的java驱动操作,具体代码如下所示:
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; publicclassMongoZyh{ publicstaticvoidmain(String[]args){ try{ //连接到MongoDB服务,ServerAddress()两个参数分别为服务器地址和端口 ServerAddressserverAddress=newServerAddress("localhost",27017); List<ServerAddress>addrs=newArrayList<ServerAddress>(); addrs.add(serverAddress); //三个参数分别为用户名数据库名称密码 MongoCredentialcredential=MongoCredential .createScramSha1Credential("zyh","admin", "zyh".toCharArray()); List<MongoCredential>credentials=newArrayList<MongoCredential>(); credentials.add(credential); //通过连接认证获取MongoDB连接 MongoClientmongoClient=newMongoClient(addrs,credentials); //连接到数据库 MongoDatabasemongoDatabase=mongoClient.getDatabase("zyhdb"); //新建集合,执行后会在数据库里新建一个空的集合 //mongoDatabase.createCollection("student"); //System.out.println("新建集合成功"); //获取集合,并往集合中插入数据 MongoCollection<Document>mongoCollection=mongoDatabase .getCollection("student"); //插入一条数据 //Documentdocument=newDocument(); //document.append("name","zhangsan"); //document.append("age",28); //mongoCollection.insertOne(document); //System.out.println("插入一条数据成功"); //插入多条数据 //List<Document>documentList=newArrayList<Document>(); //Documentdocument1=newDocument(); //document1.append("name","lisi"); //document1.append("age",28); //document1.append("sex","男"); //Documentdocument2=newDocument(); //document2.append("name","wangwu"); //document2.append("age",31); //document2.append("sex","男"); //documentList.add(document1); //documentList.add(document2); //mongoCollection.insertMany(documentList); //System.out.println("插入多条数据成功"); //查询数据 //查询集合中所有的数据 //FindIterable<Document>findIterable=mongoCollection.find(); //MongoCursor<Document>mongoCursor=findIterable.iterator(); //while(mongoCursor.hasNext()){ //System.out.println(mongoCursor.next()); //} //根据条件查询 //Documentquery=newDocument(); //query.put("age",newDocument("$lt",30)); //query.put("sex","男"); //query.put("name",query); //正则表达式查询 //Patternpattern=Pattern.compile("^zhang"); //query.put("name",pattern); //排序 //Documentsort=newDocument(); //sort.put("name",-1);//1是正序,-1是倒序 //FindIterable<Document>findIterable=mongoCollection.find(query) //.sort(sort); //MongoCursor<Document>mongoCursor=findIterable.iterator(); //while(mongoCursor.hasNext()){ //Documentdoc=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(查询条件,替换内容);//查询出第一条数据并替换 //修改数据 //Documentquery=newDocument(); //query.put("age",28); //Documentupdate=newDocument(); //Documentd=newDocument(); //d.put("birthday",newDate()); //d.put("name","zhangsan"); //update.put("$set",d); //mongoCollection.updateOne(query,update);//修改查询到的第一条数据 //mongoCollection.updateMany(查询条件,修改内容);//修改查询到的所有数据 //删除数据 //Documentquery=newDocument(); //query.put("age",28); //mongoCollection.deleteOne(query);//删除查询到的第一条数据 //mongoCollection.deleteMany(查询条件);//删除查询到的所有数据 //mongoCollection.drop();//删除集合 }catch(Exceptione){ e.printStackTrace(); } } }
关于小编给大家介绍的MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解就给大家介绍这么多,希望对大家有所帮助,还会给大家持续更新MongoDB相关知识,敬请关注毛票票网站!