forked from techforum-repo/youttubedata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDBAtlasClient.java
30 lines (21 loc) · 982 Bytes
/
MongoDBAtlasClient.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
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.types.ObjectId;
public class MongoDBAtlasClient {
public static void main(String[] args) {
//System.setProperty("jdk.tls.trustNameService", "true");
MongoClientURI uri = new MongoClientURI(
"mongodb+srv://<user name>:<password>@<cluster hostname>/test?retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=30000");
try(MongoClient mongoClient = new MongoClient(uri))
{
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("test");
Document query = new Document("_id", new ObjectId("5e234fe121fcf183e83ddce2"));
Document result = collection.find(query).iterator().next();
System.out.println("Test3: "+result.getString("test3"));
}
}
}