Skip to content

Commit 9d38fea

Browse files
committed
add date range examples
1 parent 7abfefa commit 9d38fea

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Projections.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
import java.time.Instant
8+
import java.util.*
9+
10+
fun main() {
11+
val uri = "<connection string>"
12+
val mongoClient = MongoClient.create(uri)
13+
val database = mongoClient.getDatabase("sample_mflix")
14+
val collection = database.getCollection<Document>("movies")
15+
16+
runBlocking {
17+
val mustClauses = listOf(
18+
Document(
19+
"range", Document("path", "released")
20+
.append("gt", Date.from(Instant.parse("2015-01-01T00:00:00.000Z")))
21+
.append("lt", Date.from(Instant.parse("2015-12-31T00:00:00.000Z")))
22+
)
23+
)
24+
25+
val shouldClauses = listOf(
26+
Document(
27+
"near",
28+
Document("pivot", 2629800000L)
29+
.append("path", "released")
30+
.append("origin", Date.from(Instant.parse("2015-07-01T00:00:00.000+00:00")))
31+
)
32+
)
33+
34+
val agg = Document(
35+
"\$search",
36+
Document("index", "date-range-tutorial")
37+
.append(
38+
"compound",
39+
Document().append("must", mustClauses)
40+
.append("should", shouldClauses)
41+
)
42+
)
43+
44+
val resultsFlow = collection.aggregate<Document>(
45+
listOf(
46+
agg,
47+
limit(6),
48+
project(fields(
49+
excludeId(),
50+
include("title", "released", "genres"),
51+
computed("score", Document("\$meta", "searchScore"))
52+
))
53+
)
54+
)
55+
56+
resultsFlow.collect { println(it) }
57+
}
58+
59+
mongoClient.close()
60+
}
61+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import com.mongodb.client.model.Aggregates.limit
2+
import com.mongodb.client.model.Aggregates.project
3+
import com.mongodb.client.model.Projections.*
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
import java.time.Instant
8+
import java.util.*
9+
10+
fun main() {
11+
val uri = "<connection string>"
12+
val mongoClient = MongoClient.create(uri)
13+
val database = mongoClient.getDatabase("sample_mflix")
14+
val collection = database.getCollection<Document>("movies")
15+
16+
runBlocking {
17+
val mustClauses = listOf(
18+
Document(
19+
"range", Document("path", "released")
20+
.append("gt", Date.from(Instant.parse("2015-01-01T00:00:00.000Z")))
21+
.append("lt", Date.from(Instant.parse("2015-12-31T00:00:00.000Z")))
22+
)
23+
)
24+
25+
val shouldClauses = listOf(
26+
Document(
27+
"near",
28+
Document("pivot", 2629800000L)
29+
.append("path", "released")
30+
.append("origin", Date.from(Instant.parse("2015-07-01T00:00:00.000+00:00")))
31+
)
32+
)
33+
34+
val mustNotClauses = listOf(
35+
Document(
36+
"text",
37+
Document("query", "Documentary")
38+
.append("path", "genres")
39+
)
40+
)
41+
42+
val agg = Document(
43+
"\$search",
44+
Document("index", "date-range-tutorial")
45+
.append(
46+
"compound",
47+
Document().append("must", mustClauses)
48+
.append("should", shouldClauses)
49+
.append("mustNot", mustNotClauses)
50+
)
51+
)
52+
53+
val resultsFlow = collection.aggregate<Document>(
54+
listOf(
55+
agg,
56+
limit(6),
57+
project(fields(
58+
excludeId(),
59+
include("title", "released", "genres"),
60+
computed("score", Document("\$meta", "searchScore"))
61+
))
62+
)
63+
)
64+
65+
resultsFlow.collect { println(it) }
66+
}
67+
68+
mongoClient.close()
69+
}
70+

0 commit comments

Comments
 (0)