mongodb与java整合需要用到mongodb驱动,如果是maven环境,则添加如下倚赖:
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.2.2</version> </dependency>
如果是普通的项目,就下载对应的jar包添加到lib目录下面。
ServerAddress serverAddress = new ServerAddress("localhost", 27017); List<ServerAddress> addresses = new ArrayList<ServerAddress>(); addresses.add(serverAddress); // mogodb开启的权限验证 // 类似 db.auth("userName", "pwd") MongoCredential credential = MongoCredential.createCredential("root", "test", "000000".toCharArray()); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add(credential); MongoClient client = new MongoClient(addresses, credentials); // 链接到数据库 // use test MongoDatabase mongoDatabase = client.getDatabase("test"); log.info("创建链接成功"); //获取集合 MongoCollection<Document> item = mongoDatabase.getCollection("item_info"); //像集合中插入元素 /** * 1. 创建文档 org.bson.Document 参数为key-value的格式 * 2. 创建文档集合List<Document> * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document) * */ List<Document> documents = new ArrayList<Document>(); Long startTimeStamp = System.currentTimeMillis(); for(int i = 0; i < 10; i++){ Document document = new Document("item_id", UUID.randomUUID().toString()) .append("item_name", "itemName_" + i) .append("sku", "sku_" + i) .append("price", Double.parseDouble("5") + i) .append("desc", "desc_" + "item_name_" + i ); //documents.add(document); item.insertOne(document); } // item.insertMany(documents); Long endTimeStamp = System.currentTimeMillis(); System.out.println("============="); System.out.println("耗费时间:" + ((endTimeStamp - startTimeStamp)) / 1000.0); System.out.println("============="); //查询 // Bson condition = // 查询价格小于 200 的商品 FindIterable<Document> results = item.find(Filters.lt("price", 10)); MongoCursor<Document> iterator = results.iterator(); int i = 0; if(null != results && iterator.hasNext()){ while(iterator.hasNext()){ System.out.println("==================="); Document result = iterator.next(); System.out.println(result.get("item_id")); System.out.println(result.get("item_name")); System.out.println(result.get("price")); System.out.println("==================="); i++; } } // 查总数 System.out.println(i++); System.out.println("<--------------------------->\n"); //更新操作 // 更新操作是针对于collection上面的函数, UpdateResult s = item.updateMany(Filters.lt("price", 10), new Document("$set", new Document(). append("price", 11.5) .append("item_name", "update_item_name"))); //查询 FindIterable<Document> updateItems = item.find(Filters.eq("item_name", "update_item_name")); MongoCursor<Document> updateIterator = updateItems.iterator(); i = 0; if(null != updateIterator){ while(updateIterator.hasNext()){ Document updateItem = updateIterator.next(); System.out.println("==================="); System.out.println(updateItem.get("item_id")); System.out.println(updateItem.get("item_name")); System.out.println(updateItem.get("price")); System.out.println("==================="); i++; } } System.out.println(s.getModifiedCount()); // 删除 DeleteResult delete = item.deleteMany(Filters.eq("item_name", "update_item_name")); System.out.println(delete.getDeletedCount());
时间: 2024-10-13 15:29:30