BAEL-5833 | Article code (#12919)
* BAEL-5833 | Article code * Updated method name
This commit is contained in:
parent
ca8e33e339
commit
c57284d9c9
@ -22,7 +22,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mongo.version>3.12.1</mongo.version>
|
||||
<mongo.version>3.12.11</mongo.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,134 @@
|
||||
package com.baeldung.mongo.filter;
|
||||
|
||||
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 {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase database;
|
||||
private static MongoCollection<Document> collection;
|
||||
private static String collectionName;
|
||||
private static String databaseName;
|
||||
|
||||
public static void setUp() {
|
||||
if (mongoClient == null) {
|
||||
mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
databaseName = "baeldung";
|
||||
collectionName = "user";
|
||||
|
||||
database = mongoClient.getDatabase(databaseName);
|
||||
collection = database.getCollection(collectionName);
|
||||
}
|
||||
}
|
||||
|
||||
public static void equalsOperator() {
|
||||
Bson filter = eq("userName", "Jack");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void notEqualOperator() {
|
||||
Bson filter = ne("userName", "Jack");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void greaterThanOperator() {
|
||||
Bson filter = gt("age", 25);
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void lessThanOperator() {
|
||||
Bson filter = lt("age", 25);
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void inOperator() {
|
||||
Bson filter = in("userName", "Jack", "Lisa");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void notInOperator() {
|
||||
Bson filter = nin("userName", "Jack", "Lisa");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void andOperator() {
|
||||
Bson filter = and(gt("age", 25), eq("role", "Admin"));
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void orOperator() {
|
||||
Bson filter = or(gt("age", 30), eq("role", "Admin"));
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void existsOperator() {
|
||||
Bson filter = exists("type");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
public static void regexOperator() {
|
||||
Bson filter = regex("userName", "a");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
|
||||
printResult(documents);
|
||||
}
|
||||
|
||||
private static void printResult(FindIterable<Document> documents) {
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
while (cursor.hasNext()) {
|
||||
System.out.println(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
setUp();
|
||||
|
||||
equalsOperator();
|
||||
|
||||
notEqualOperator();
|
||||
|
||||
greaterThanOperator();
|
||||
|
||||
lessThanOperator();
|
||||
|
||||
inOperator();
|
||||
|
||||
notInOperator();
|
||||
|
||||
andOperator();
|
||||
|
||||
orOperator();
|
||||
|
||||
existsOperator();
|
||||
|
||||
regexOperator();
|
||||
}
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
package com.baeldung.mongo.filter;
|
||||
|
||||
import com.baeldung.mongo.find.FindOperationLiveTest;
|
||||
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 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 static com.mongodb.client.model.Filters.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class FilterOperationLiveTest {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase database;
|
||||
private static MongoCollection<Document> collection;
|
||||
private static final String DATASET_JSON = "/user.json";
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
if (mongoClient == null) {
|
||||
mongoClient = new MongoClient("localhost", 27017);
|
||||
|
||||
database = mongoClient.getDatabase("baeldung");
|
||||
collection = database.getCollection("user");
|
||||
|
||||
collection.drop();
|
||||
|
||||
InputStream is = FindOperationLiveTest.class.getResourceAsStream(DATASET_JSON);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
reader.lines()
|
||||
.forEach(line -> collection.insertOne(Document.parse(line)));
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingEqualsOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = eq("userName", "Jack");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingNotEqualOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = ne("userName", "Jack");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingGreaterThanOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = gt("age", 25);
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingLessThanOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = lt("age", 25);
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingInOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = in("userName", "Jack", "Lisa");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingNotInOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = nin("userName", "Jack", "Lisa");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingAndOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = and(gt("age", 25), eq("role", "Admin"));
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingOrOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = or(gt("age", 30), eq("role", "Admin"));
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingExistsOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = exists("type");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserCollection_whenFetchingUsingRegexOperator_thenFindMatchingDocuments() {
|
||||
Bson filter = regex("userName", "a");
|
||||
FindIterable<Document> documents = collection.find(filter);
|
||||
MongoCursor<Document> cursor = documents.iterator();
|
||||
|
||||
assertNotNull(cursor);
|
||||
assertTrue(cursor.hasNext());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanUp() {
|
||||
mongoClient.close();
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
{"userId":"123","userName":"Jack","age":23,"role":"Admin"}
|
||||
{"userId":"456","userName":"Lisa","age":27,"role":"Admin","type":"Web"}
|
||||
{"userId":"789","userName":"Tim","age":31,"role":"Analyst"}
|
Loading…
x
Reference in New Issue
Block a user