Skip to content

Commit 970678b

Browse files
committed
Adding app.java update
1 parent 6c01514 commit 970678b

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
package com.napier.sem;
22

3+
import com.mongodb.MongoClient;
4+
import com.mongodb.client.MongoDatabase;
5+
import com.mongodb.client.MongoCollection;
6+
import org.bson.Document;
7+
38
public class App
49
{
510
public static void main(String[] args)
611
{
7-
System.out.println("Boo yah!");
12+
// Connect to MongoDB on local system - we're using port 27000
13+
MongoClient mongoClient = new MongoClient("localhost", 27000);
14+
// Get a database - will create when we use it
15+
MongoDatabase database = mongoClient.getDatabase("mydb");
16+
// Get a collection from the database
17+
MongoCollection<Document> collection = database.getCollection("test");
18+
// Create a document to store
19+
Document doc = new Document("name", "Kevin Sim")
20+
.append("class", "DevOps")
21+
.append("year", "2024")
22+
.append("result", new Document("CW", 95).append("EX", 85));
23+
// Add document to collection
24+
collection.insertOne(doc);
825

26+
// Check document in collection
27+
Document myDoc = collection.find().first();
28+
System.out.println(myDoc.toJson());
929
}
10-
1130
}

0 commit comments

Comments
 (0)