Java で MongoDB に接続する一般的な方法の分析例
1. MongoDB への Java リンク
1. Mongo ドライバー パッケージをインポートします
2. Mongo リンク オブジェクトを取得します
MongoClient mc = new MongoClient("localhost",27017);
3. リンクを閉じます
mc.close();
2. ライブラリを表示し、コレクションを表示します
1. ライブラリ オブジェクトを取得します
MongoDatabase db = mc.getDatabase("myschool");
2. テーブルのコレクションを取得しますlibrary
MongoIterable<String> listCollectionNames = db.listCollectionNames(); MongoCursor<String> iterator = listCollectionNames.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
3 、Java は MongoDB クエリを追加、削除、および変更します
1. データを追加します
#a. データを 1 つ追加します#
//创建对象 Student s = new Student(); s.setSid(1); s.setSname("王俊凯"); s.setBirthday(new Date()); s.setSsex("男"); s.setClassid(2); //将数据转换为json格式 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); String json = gson.toJson(s); //获取集合对象 MongoCollection<Document> collection = db.getCollection("student"); //添加一条数据,将json格式转换为document对象 collection.insertOne(Document.parse(json));
//存入数据 List<Document> dlist=new ArrayList<Document>(); for(int i=0; i<3; i++){ Student s = new Student(); s.setSid(Integer.toString(i+1)); s.setSname("王源"); s.setBirthday(new Date()); s.setSsex("男"); s.setClassid(1); //将数据转换为json格式 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); String json = gson.toJson(s); dlist.add(Document.parse(json)); } //获取集合对象 MongoCollection<Document> collection = db.getCollection("student"); //添加多条数据 collection.insertMany(dlist);
//获取集合对象 MongoCollection<Document> collection = db.getCollection("student"); Student s = new Student(); s.setSid(1); Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); Bson bson = Document.parse(gson.toJson(s)); DeleteResult deleteOne = collection.deleteOne(bson);
//获取集合对象 MongoCollection<Document> collection = db.getCollection("student"); Student s = new Student(); s.setSname("王源"); Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); Bson bson = Document.parse(gson.toJson(s)); DeleteResult deleteMany = collection.deleteMany(bson);
MongoCollection<Document> collection = db.getCollection("student"); //一个条件对象 Bson eq = Filters.eq("sname","易烊千玺"); //要修改的数据 Document doc = new Document(); doc.put("$set", new Document("age",22)); UpdateResult updateone = collection.updateOne(eq, doc); System.out.println(updateone);
#
MongoCollection<Document> collection = db.getCollection("student"); //多条件 Bson bson = Filters.and(Filters.gte("age", 20),Filters.lte("age", 40)); //要修改的数据 Document doc = new Document(); doc.put("$set", new Document("sex","男")); UpdateResult updateMany = collection.updateMany(bson, doc); System.out.println(updateMany);
MongoCollection<Document> collection = db.getCollection("student"); FindIterable<Document> findAll = collection.find(); MongoCursor<Document> iterator = findAll.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); }
MongoCollection<Document> collection = db.getCollection("student"); //一个条件对象 Bson eq = Filters.eq("sname","易烊千玺"); FindIterable<Document> findOne = collection.find(eq); MongoCursor<Document> iterator = findOne.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); }
MongoCollection<Document> collection = db.getCollection("student"); //使用正则表达式进行模糊查找 Bson eq = Filters.regex("sname","易"); FindIterable<Document> find = collection.find(eq); MongoCursor<Document> iterator = find.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); }
MongoCollection<Document> collection = db.getCollection("student"); //分页查询 FindIterable<Document> find = collection.find().skip(2).limit(3); MongoCursor<Document> iterator = find.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); }
リーリー
以上がJava で MongoDB に接続する一般的な方法の分析例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Java の乱数ジェネレーターのガイド。ここでは、Java の関数について例を挙げて説明し、2 つの異なるジェネレーターについて例を挙げて説明します。

Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

Java のアームストロング番号に関するガイド。ここでは、Java でのアームストロング数の概要とコードの一部について説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです
