-
Notifications
You must be signed in to change notification settings - Fork 2
/
basicTest.java
54 lines (48 loc) · 2.1 KB
/
basicTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//package com.ibm.nosql.wireListener.test.db2UnitTests;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import java.util.Arrays;
import com.mongodb.Block;
import com.mongodb.client.MongoCursor;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Updates.*;
import com.mongodb.client.result.UpdateResult;
import java.util.ArrayList;
import java.util.List;
class basicTest {
public static void main(String[] args) {
System.out.println("Hello, World!");
MongoClientOptions mongoClientOptions = MongoClientOptions.builder()
.serverSelectionTimeout(500000)
.build();
//MongoClient mongoClient = new MongoClient(new ServerAddress("localhost"), mongoClientOptions);
//MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("book");
Document doc = new Document("name", "MongoDB")
.append("type", "database");
collection.insertOne(doc);
for (String name : database.listCollectionNames()) {
System.out.println("Collection Name = " + name);
}
System.out.println("Document Count = " + collection.countDocuments());
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
DeleteResult deleteResult = collection.deleteMany(eq("name", "MongoDB"));
System.out.println("Deleted doc count = " + deleteResult.getDeletedCount());
}
}