File tree Expand file tree Collapse file tree
src/main/java/com/napier/sem Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package 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+
38public 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}
You can’t perform that action at this time.
0 commit comments