0
# Database Operations
1
2
Database-level operations including collection management, command execution, and database administration with support for sessions and read/write preferences.
3
4
## Capabilities
5
6
### MongoDatabase Interface
7
8
Main interface for database operations providing collection access, database management, and MongoDB command execution.
9
10
```java { .api }
11
public interface MongoDatabase {
12
// Database metadata
13
String getName();
14
CodecRegistry getCodecRegistry();
15
ReadPreference getReadPreference();
16
WriteConcern getWriteConcern();
17
ReadConcern getReadConcern();
18
19
// Configuration variants
20
MongoDatabase withCodecRegistry(CodecRegistry codecRegistry);
21
MongoDatabase withReadPreference(ReadPreference readPreference);
22
MongoDatabase withWriteConcern(WriteConcern writeConcern);
23
MongoDatabase withReadConcern(ReadConcern readConcern);
24
25
// Collection access
26
MongoCollection<Document> getCollection(String collectionName);
27
<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass);
28
29
// Collection management
30
void createCollection(String collectionName);
31
void createCollection(String collectionName, CreateCollectionOptions options);
32
void createCollection(ClientSession clientSession, String collectionName);
33
void createCollection(ClientSession clientSession, String collectionName, CreateCollectionOptions options);
34
35
// View management
36
void createView(String viewName, String viewOn, List<? extends Bson> pipeline);
37
void createView(String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions options);
38
void createView(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline);
39
void createView(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions options);
40
41
// Collection listing
42
MongoIterable<String> listCollectionNames();
43
MongoIterable<String> listCollectionNames(ClientSession clientSession);
44
ListCollectionsIterable<Document> listCollections();
45
<TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> resultClass);
46
ListCollectionsIterable<Document> listCollections(ClientSession clientSession);
47
<TResult> ListCollectionsIterable<TResult> listCollections(ClientSession clientSession, Class<TResult> resultClass);
48
49
// Command execution
50
Document runCommand(Bson command);
51
Document runCommand(Bson command, ReadPreference readPreference);
52
<TResult> TResult runCommand(Bson command, Class<TResult> resultClass);
53
<TResult> TResult runCommand(Bson command, ReadPreference readPreference, Class<TResult> resultClass);
54
Document runCommand(ClientSession clientSession, Bson command);
55
Document runCommand(ClientSession clientSession, Bson command, ReadPreference readPreference);
56
<TResult> TResult runCommand(ClientSession clientSession, Bson command, Class<TResult> resultClass);
57
<TResult> TResult runCommand(ClientSession clientSession, Bson command, ReadPreference readPreference, Class<TResult> resultClass);
58
59
// Database management
60
void drop();
61
void drop(ClientSession clientSession);
62
63
// Change streams
64
ChangeStreamIterable<Document> watch();
65
<TResult> ChangeStreamIterable<TResult> watch(Class<TResult> resultClass);
66
ChangeStreamIterable<Document> watch(List<? extends Bson> pipeline);
67
<TResult> ChangeStreamIterable<TResult> watch(List<? extends Bson> pipeline, Class<TResult> resultClass);
68
ChangeStreamIterable<Document> watch(ClientSession clientSession);
69
<TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, Class<TResult> resultClass);
70
ChangeStreamIterable<Document> watch(ClientSession clientSession, List<? extends Bson> pipeline);
71
<TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass);
72
73
// Database-level aggregation
74
AggregateIterable<Document> aggregate(List<? extends Bson> pipeline);
75
<TResult> AggregateIterable<TResult> aggregate(List<? extends Bson> pipeline, Class<TResult> resultClass);
76
AggregateIterable<Document> aggregate(ClientSession clientSession, List<? extends Bson> pipeline);
77
<TResult> AggregateIterable<TResult> aggregate(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass);
78
}
79
80
// View creation options
81
public class CreateViewOptions {
82
public CreateViewOptions collation(Collation collation);
83
public Collation getCollation();
84
}
85
```
86
87
### Collection Management
88
89
Creating and managing MongoDB collections and views within databases.
90
91
**Usage Examples:**
92
93
```java
94
import com.mongodb.client.MongoDatabase;
95
import com.mongodb.client.model.CreateCollectionOptions;
96
import com.mongodb.client.model.ValidationOptions;
97
import com.mongodb.client.model.Filters;
98
99
// Create a simple collection
100
database.createCollection("users");
101
102
// Create collection with options
103
CreateCollectionOptions options = new CreateCollectionOptions()
104
.capped(true)
105
.sizeInBytes(1000000)
106
.maxDocuments(1000);
107
database.createCollection("logs", options);
108
109
// Create a view
110
database.createView("activeUsers", "users", Arrays.asList(
111
Aggregates.match(Filters.eq("status", "active"))
112
));
113
```
114
115
### Command Execution
116
117
Direct execution of MongoDB database commands with flexible result handling.
118
119
**Usage Examples:**
120
121
```java
122
import org.bson.Document;
123
124
// Execute a ping command
125
Document result = database.runCommand(new Document("ping", 1));
126
127
// Execute with read preference
128
Document stats = database.runCommand(
129
new Document("dbStats", 1),
130
ReadPreference.secondary()
131
);
132
133
// Execute with custom result class
134
MyCustomResult result = database.runCommand(
135
new Document("customCommand", 1),
136
MyCustomResult.class
137
);
138
```
139
140
### Database-Level Aggregation
141
142
Run aggregation pipelines at the database level for operations that don't require a specific collection.
143
144
**Usage Examples:**
145
146
```java
147
import com.mongodb.client.model.Aggregates;
148
149
// List current operations
150
AggregateIterable<Document> currentOps = database.aggregate(Arrays.asList(
151
new Document("$currentOp", new Document())
152
));
153
154
// List local sessions
155
AggregateIterable<Document> sessions = database.aggregate(Arrays.asList(
156
new Document("$listLocalSessions", new Document())
157
));
158
```