目次
1. MongoDB への Java リンク
2. ライブラリを表示し、コレクションを表示します
3 、Java は MongoDB クエリを追加、削除、および変更します
1. データを追加します
ホームページ Java &#&チュートリアル Java で MongoDB に接続する一般的な方法の分析例

Java で MongoDB に接続する一般的な方法の分析例

May 26, 2023 pm 07:06 PM
java mongodb

1. MongoDB への Java リンク

1. Mongo ドライバー パッケージをインポートします

Java で MongoDB に接続する一般的な方法の分析例

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));
ログイン後にコピー

b。複数のデータを追加

//存入数据
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);
ログイン後にコピー

2.データを削除

a.1つのデータを削除

//获取集合对象
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);
ログイン後にコピー

b.複数のデータを削除

//获取集合对象
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);
ログイン後にコピー

3. データの変更

a. 1 つのデータの変更

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);
ログイン後にコピー

b. 複数のデータの変更

#

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);
ログイン後にコピー

4. データのクエリ

#a.すべて検索

MongoCollection<Document> collection = db.getCollection("student");
 
FindIterable<Document> findAll = collection.find();
 
MongoCursor<Document> iterator = findAll.iterator();
 
while(iterator.hasNext()){
   System.out.println(iterator.next()); 
}
ログイン後にコピー

b. 条件付きクエリ

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()); 
}
ログイン後にコピー

c. ファジークエリ

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()); 
}
ログイン後にコピー

d. ページングクエリ

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()); 
}
ログイン後にコピー
e. 並べ替えクエリ

リーリー

以上がJava で MongoDB に接続する一般的な方法の分析例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

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

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

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

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

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

Javaの平方根 Javaの平方根 Aug 30, 2024 pm 04:26 PM

Java の平方根のガイド。ここでは、Java で平方根がどのように機能するかを、例とそのコード実装をそれぞれ示して説明します。

Javaの完全数 Javaの完全数 Aug 30, 2024 pm 04:28 PM

Java における完全数のガイド。ここでは、定義、Java で完全数を確認する方法、コード実装の例について説明します。

Java の乱数ジェネレーター Java の乱数ジェネレーター Aug 30, 2024 pm 04:27 PM

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

ジャワのウェカ ジャワのウェカ Aug 30, 2024 pm 04:28 PM

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

Javaのアームストロング数 Javaのアームストロング数 Aug 30, 2024 pm 04:26 PM

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

Javaのスミス番号 Javaのスミス番号 Aug 30, 2024 pm 04:28 PM

Java のスミス番号のガイド。ここでは定義、Java でスミス番号を確認する方法について説明します。コード実装の例。

Java Springのインタビューの質問 Java Springのインタビューの質問 Aug 30, 2024 pm 04:29 PM

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

Java 8 Stream Foreachから休憩または戻ってきますか? Java 8 Stream Foreachから休憩または戻ってきますか? Feb 07, 2025 pm 12:09 PM

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

See all articles