Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA-41260] #18181

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions persistence-modules/java-mongodb-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

This module contains articles about MongoDB in Java.

- [Guide to Upsert in MongoDB](https://www.baeldung.com/mongodb-upsert)
- [Bulk Update of Documents in MongoDB](https://www.baeldung.com/mongodb-bulk-update-documents)
- [Case Insensitive Sorting in MongoDB](https://www.baeldung.com/java-mongodb-case-insensitive-sorting)
- [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists)
- [Push Operations in MongoDB](https://www.baeldung.com/mongodb-push-operations)
- [Geospatial Support in MongoDB](https://www.baeldung.com/mongodb-geospatial-support)
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
- [Retrieve a Value from MongoDB by Its Key Name](https://www.baeldung.com/mongodb-get-value-by-key-name)
- [Push and Set Operations in Same MongoDB Update](https://www.baeldung.com/java-mongodb-push-set)
- [Checking Connection to MongoDB](https://www.baeldung.com/mongodb-check-connection)
- [MongoDB BSON Guide](https://www.baeldung.com/mongodb-bson)
- More articles: [[<-- prev]](../java-mongodb)
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.baeldung;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bson.Document;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MongoBsonExample
{
Expand Down

This file was deleted.

6 changes: 5 additions & 1 deletion persistence-modules/java-mongodb-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
- [Guide to Find in MongoDB](https://www.baeldung.com/mongodb-find)
- [Query Documents using Document ID in MongoDB](https://www.baeldung.com/mongodb-query-documents-id)
- [Insert Array Inside an Object in MongoDB](https://www.baeldung.com/java-mongodb-document-insert-array)
- [Guide to Filters in MongoDB](https://www.baeldung.com/java-mongodb-filters)
- [Add Field to an Existing MongoDB Bson Filter in Java](https://www.baeldung.com/java-mongodb-add-field-bson-filter)
- [A Simple Tagging Implementation with MongoDB](http://www.baeldung.com/mongodb-tagging)
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
- [Check Collection Existence in MongoDB](https://www.baeldung.com/java-check-collection-existence-mongodb)
- [Get Last Inserted Document ID in MongoDB With Java Driver](https://www.baeldung.com/java-mongodb-last-inserted-id)
- More articles: [[<-- prev]](../java-mongodb-2)
6 changes: 6 additions & 0 deletions persistence-modules/java-mongodb-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
<artifactId>mongo-java-driver</artifactId>
<version>${mongo.version}</version>
</dependency>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
<version>${morphia.version}</version>
</dependency>
</dependencies>

<properties>
<mongo.version>3.12.11</mongo.version>
<morphia.version>2.0.0</morphia.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.mongo;
package com.baeldung.mongo.collectionexistence;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.morphia.domain;
package com.baeldung.mongo.morphia.domain;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.baeldung.morphia.domain;
package com.baeldung.mongo.morphia.domain;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

import dev.morphia.annotations.Embedded;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Field;
import dev.morphia.annotations.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.morphia.domain;
package com.baeldung.mongo.morphia.domain;

import org.bson.types.ObjectId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.tagging;
package com.baeldung.mongo.tagging;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.tagging;
package com.baeldung.mongo.tagging;

import java.io.Closeable;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.mongo;
package com.baeldung.mongo.collectionexistence;

import static org.junit.Assert.assertEquals;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.morphia;
package com.baeldung.mongo.morphia;

import static dev.morphia.aggregation.Group.grouping;
import static dev.morphia.aggregation.Group.push;
Expand All @@ -17,9 +17,9 @@
import org.junit.BeforeClass;
import org.junit.Test;

import com.baeldung.morphia.domain.Author;
import com.baeldung.morphia.domain.Book;
import com.baeldung.morphia.domain.Publisher;
import com.baeldung.mongo.morphia.domain.Author;
import com.baeldung.mongo.morphia.domain.Book;
import com.baeldung.mongo.morphia.domain.Publisher;
import com.mongodb.client.MongoClients;
import com.mongodb.client.model.ReturnDocument;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.tagging;
package com.baeldung.mongo.tagging;

import java.io.IOException;
import java.util.Arrays;
Expand Down
9 changes: 4 additions & 5 deletions persistence-modules/java-mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ This module contains articles about MongoDB in Java.
### Relevant articles:

- [A Guide to MongoDB with Java](http://www.baeldung.com/java-mongodb)
- [A Simple Tagging Implementation with MongoDB](http://www.baeldung.com/mongodb-tagging)
- [MongoDB BSON Guide](https://www.baeldung.com/mongodb-bson)
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
- [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json)
- [Get Last Inserted Document ID in MongoDB With Java Driver](https://www.baeldung.com/java-mongodb-last-inserted-id)
- [Update Multiple Fields in a MongoDB Document](https://www.baeldung.com/mongodb-update-multiple-fields)
- [Update Documents in MongoDB](https://www.baeldung.com/mongodb-update-documents)
- [Check Collection Existence in MongoDB](https://www.baeldung.com/java-check-collection-existence-mongodb)
- [Guide to Filters in MongoDB](https://www.baeldung.com/java-mongodb-filters)
- [Checking Connection to MongoDB](https://www.baeldung.com/mongodb-check-connection)
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
- [Guide to Upsert in MongoDB](https://www.baeldung.com/mongodb-upsert)
- More articles: [next -->](../java-mongodb-2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baeldung.mongo.connectioncheck;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import org.bson.Document;

import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;

public class ConnectionCheck {

public static MongoClient checkingConnection() {

MongoClientSettings settings = MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Collections.singletonList(new ServerAddress("localhost", 27017))))
.applyToConnectionPoolSettings(builder -> {
builder.maxSize(100);
builder.maxWaitTime(60000, TimeUnit.MILLISECONDS);
})
.applyToSocketSettings(builder -> {
builder.connectTimeout(1500,TimeUnit.MILLISECONDS);
builder.readTimeout(60000,TimeUnit.MILLISECONDS);
})
.build();

MongoClient mongoClient = MongoClients.create(settings);

try {
System.out.println("MongoDB Server is Up");

MongoDatabase database = mongoClient.getDatabase("baeldung");

Document stats = database.runCommand(new Document("dbStats", 1));
System.out.println(stats.toJson());

} catch (Exception e) {
System.out.println("MongoDB Server is Down");
e.printStackTrace();
}

return mongoClient;

}

public static void main(String[] args) {
//
// Connection check
//
checkingConnection();
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.baeldung.mongo.filter;

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.exists;
import static com.mongodb.client.model.Filters.gt;
import static com.mongodb.client.model.Filters.in;
import static com.mongodb.client.model.Filters.lt;
import static com.mongodb.client.model.Filters.ne;
import static com.mongodb.client.model.Filters.nin;
import static com.mongodb.client.model.Filters.or;
import static com.mongodb.client.model.Filters.regex;

import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.conversions.Bson;

import static com.mongodb.client.model.Filters.*;

public class FilterOperation {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
package com.baeldung.mongo.aggregation;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

import static com.mongodb.client.model.Aggregates.count;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.limit;
Expand All @@ -31,6 +11,27 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;

public class AggregationLiveTest {

private static final String DATABASE = "world";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.baeldung.mongo;
package com.baeldung.mongo.connectioncheck;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;

import com.baeldung.ConnectionCheck;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.connection.ServerDescription;
import com.mongodb.client.MongoClient;

public class ConnectionCheckLiveTest {

Expand All @@ -16,7 +17,15 @@ public class ConnectionCheckLiveTest {
public void givenMongoClient_whenConnectionCheck_thenCheckingForConnectionPoint() {

MongoClient mongoClient = ConnectionCheck.checkingConnection();
String connectionPoint = mongoClient.getConnectPoint();

ServerAddress serverAddress = mongoClient.getClusterDescription()
.getServerDescriptions()
.stream()
.findFirst()
.map(ServerDescription::getAddress)
.orElse(null);

String connectionPoint = serverAddress.toString();
assertNotNull(connectionPoint);
assertFalse(connectionPoint.isEmpty());

Expand Down
Loading