-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCSP-41455: Standardize Count Documents page (#22)
(cherry picked from commit 6bd92e0)
- Loading branch information
Showing
2 changed files
with
93 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import com.mongodb.ConnectionString | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.client.model.* | ||
import com.mongodb.client.model.Filters.* | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.codecs.pojo.annotations.BsonId | ||
import org.bson.types.ObjectId | ||
|
||
// start-data-class | ||
data class Movie( | ||
@BsonId | ||
val id: ObjectId, | ||
val title: String | ||
) | ||
// end-data-class | ||
|
||
fun main() { | ||
val uri = "mongodb+srv://michael:[email protected]/?retryWrites=true&w=majority" | ||
|
||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.retryWrites(true) | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Movie>("movies") | ||
|
||
// start-count-all | ||
println(collection.countDocuments()) | ||
// end-count-all | ||
|
||
// start-count-query | ||
println(collection.countDocuments(eq("year", "1930"))) | ||
// end-count-query | ||
|
||
// start-count-options | ||
val options = CountOptions().comment("Retrieving count") | ||
collection.countDocuments(Filters.empty(), options) | ||
// end-count-options | ||
|
||
// start-estimated-count | ||
print(collection.estimatedDocumentCount()) | ||
// end-estimated-count | ||
|
||
// start-estimated-count-options | ||
val options = EstimatedDocumentCountOptions().comment("Retrieving count") | ||
collection.estimatedDocumentCount(options) | ||
// end-estimated-count-options | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters