1
+ // start-full-example
2
+ import com.mongodb.MongoException
1
3
import com.mongodb.kotlin.client.MongoClient
2
4
import org.bson.Document
5
+ import org.bson.BsonInt64
6
+ import org.bson.json.JsonWriterSettings
7
+
3
8
4
9
fun main () {
10
+ // Replace the placeholder with your MongoDB deployment's connection string
11
+ val uri = " <connection string uri>"
12
+
13
+ val mongoClient = MongoClient .create(uri)
14
+ val database = mongoClient.getDatabase(" sample_mflix" )
15
+ try {
16
+ val command = Document (" buildInfo" , BsonInt64 (1 ))
17
+ val commandResult = database.runCommand(command)
18
+ println (commandResult.toJson(JsonWriterSettings .builder().indent(true ).build()))
19
+ } catch (me: MongoException ) {
20
+ System .err.println (" An error occurred: $me " )
21
+ }
22
+ mongoClient.close()
23
+ }
24
+ // end-full-example
25
+
26
+ fun command_examples () {
5
27
// start-execute
6
- val commandToExplain = Document ( " find" , " restaurants" )
28
+ val commandToExplain = Document (" find" , " restaurants" )
7
29
val explanation = database.runCommand(Document (" explain" , commandToExplain))
8
30
// end-execute
9
31
10
32
// start-read-preference
11
33
val command = Document (" hello" , 1 )
12
- val commandReadPreference = Document ( " readPreference" , " secondary" )
34
+ val commandReadPreference = Document (" readPreference" , " secondary" )
13
35
14
36
val commandResult = database.runCommand(command, commandReadPreference)
15
37
// end-read-preference
16
38
17
39
// start-build-info
18
- println ( database.runCommand( Document ( " buildInfo" , 1 ) );
40
+ println (database.runCommand(Document (" buildInfo" , 1 ) );
19
41
// end-build-info
20
42
}
0 commit comments