Skip to content

Commit 3c4e497

Browse files
mongodbencbullingerrustagir
authored
Builders Feature Branch (#74)
# Pull Request Info Aggregating all the changes for the Builders API reference docs [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - n/a Staging - https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/builders/ ## Self-Review Checklist - [ ] Is this free of any warnings or errors in the RST? - [ ] Did you run a spell-check? - [ ] Did you run a grammar-check? - [ ] Are all the links working? --------- Co-authored-by: cbullinger <[email protected]> Co-authored-by: Rea Rustagi <[email protected]> Co-authored-by: Rea Radhika Rustagi <[email protected]>
1 parent 71ddbfd commit 3c4e497

File tree

126 files changed

+3479
-1168
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+3479
-1168
lines changed

examples/src/test/kotlin/AggregatesBuilderTest.kt

Lines changed: 936 additions & 0 deletions
Large diffs are not rendered by default.

examples/src/test/kotlin/CountTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class CountTest {
1919
companion object {
2020
val config = getConfig()
2121
val client = MongoClient.create(config.connectionUri)
22-
private val database = client.getDatabase("sample_mflix")
22+
private val database = client.getDatabase("count_test")
2323
val collection = database.getCollection<Movie>("movies")
2424

2525
@BeforeAll
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
2+
import com.mongodb.client.model.Filters
3+
import com.mongodb.client.model.geojson.Point
4+
import com.mongodb.client.model.geojson.Polygon
5+
import com.mongodb.client.model.geojson.Position
6+
import com.mongodb.kotlin.client.coroutine.MongoClient
7+
import config.getConfig
8+
import kotlinx.coroutines.flow.firstOrNull
9+
import kotlinx.coroutines.flow.toList
10+
import kotlinx.coroutines.runBlocking
11+
import org.bson.codecs.pojo.annotations.BsonId
12+
import org.junit.jupiter.api.AfterAll
13+
import org.junit.jupiter.api.Assertions.assertEquals
14+
import org.junit.jupiter.api.BeforeAll
15+
import org.junit.jupiter.api.Test
16+
17+
class FiltersBuildersTest {
18+
// :snippet-start: paint-order-data-class
19+
data class PaintOrder(
20+
@BsonId val id: Int,
21+
val qty: Int,
22+
val color: String,
23+
val vendors: List<String> = mutableListOf()
24+
)
25+
// :snippet-end:
26+
27+
companion object {
28+
val config = getConfig()
29+
val client = MongoClient.create(config.connectionUri)
30+
val database = client.getDatabase("paints_r_us")
31+
val collection = database.getCollection<PaintOrder>("paints")
32+
33+
@BeforeAll
34+
@JvmStatic
35+
fun beforeAll() {
36+
runBlocking {
37+
val paintOrders = listOf(
38+
PaintOrder(1, 5, "red", listOf("A")),
39+
PaintOrder(2, 10, "purple", listOf("C", "D")),
40+
PaintOrder(3, 8, "blue", listOf("B", "A")),
41+
PaintOrder(4, 6, "white", listOf("D")),
42+
PaintOrder(5, 11, "yellow", listOf("A", "B")),
43+
PaintOrder(6, 5, "pink", listOf("C")),
44+
PaintOrder(7, 8, "green", listOf("B", "C")),
45+
PaintOrder(8, 7, "orange", listOf("A", "D"))
46+
)
47+
collection.insertMany(paintOrders)
48+
}
49+
}
50+
51+
@AfterAll
52+
@JvmStatic
53+
fun afterAll() {
54+
runBlocking {
55+
database.drop()
56+
client.close()
57+
}
58+
}
59+
}
60+
61+
@Test
62+
fun equalComparisonTest() = runBlocking {
63+
// :snippet-start: equalComparison
64+
val equalComparison = Filters.eq(PaintOrder::qty.name, 5)
65+
val resultsFlow = collection.find(equalComparison)
66+
resultsFlow.collect { println(it) }
67+
// :snippet-end:
68+
assertEquals(2, resultsFlow.toList().size)
69+
}
70+
71+
@Test
72+
fun gteComparisonTest() = runBlocking {
73+
// :snippet-start: gteComparison
74+
val gteComparison = Filters.gte(PaintOrder::qty.name, 10)
75+
val resultsFlow = collection.find(gteComparison)
76+
resultsFlow.collect { println(it) }
77+
// :snippet-end:
78+
assertEquals(2, resultsFlow.toList().size)
79+
}
80+
81+
@Test
82+
fun emptyComparisonTest() = runBlocking {
83+
// :snippet-start: emptyComparison
84+
val emptyComparison = Filters.empty()
85+
val resultsFlow = collection.find(emptyComparison)
86+
resultsFlow.collect { println(it) }
87+
// :snippet-end:
88+
assertEquals(8, resultsFlow.toList().size)
89+
}
90+
91+
@Test
92+
fun orComparisonTest() = runBlocking {
93+
// :snippet-start: orComparison
94+
val orComparison = Filters.or(
95+
Filters.gt(PaintOrder::qty.name, 8),
96+
Filters.eq(PaintOrder::color.name, "pink")
97+
)
98+
val resultsFlow = collection.find(orComparison)
99+
resultsFlow.collect { println(it) }
100+
// :snippet-end:
101+
assertEquals(3, resultsFlow.toList().size)
102+
}
103+
104+
@Test
105+
fun allComparisonTest() = runBlocking {
106+
// :snippet-start: allComparison
107+
val search = listOf("A", "D")
108+
val allComparison = Filters.all(PaintOrder::vendors.name, search)
109+
val resultsFlow = collection.find(allComparison)
110+
resultsFlow.collect { println(it) }
111+
// :snippet-end:
112+
assertEquals(1, resultsFlow.toList().size)
113+
}
114+
115+
@Test
116+
fun existsComparisonTest() = runBlocking {
117+
// :snippet-start: existsComparison
118+
val existsComparison = Filters.and(Filters.exists(PaintOrder::qty.name), Filters.nin("qty", 5, 8))
119+
val resultsFlow = collection.find(existsComparison)
120+
resultsFlow.collect { println(it) }
121+
// :snippet-end:
122+
assertEquals(4, resultsFlow.toList().size)
123+
}
124+
125+
@Test
126+
fun regexComparisonTest() = runBlocking {
127+
// :snippet-start: regexComparison
128+
val regexComparison = Filters.regex(PaintOrder::color.name, "^p")
129+
val resultsFlow = collection.find(regexComparison)
130+
resultsFlow.collect { println(it) }
131+
// :snippet-end:
132+
assertEquals(2, resultsFlow.toList().size)
133+
}
134+
135+
@Test
136+
fun bitsComparisonTest() = runBlocking {
137+
// :snippet-start: bitsComparison
138+
data class BinaryNumber(
139+
@BsonId val id: Int,
140+
val decimalValue: Int,
141+
val binaryValue: String
142+
)
143+
val binaryCollection = database.getCollection<BinaryNumber>("binary_numbers")
144+
// :remove-start:
145+
val binaryNumbers = listOf(
146+
BinaryNumber(1, 54, "00110110"),
147+
BinaryNumber(2, 20, "00010100"),
148+
BinaryNumber(3, 68, "1000100"),
149+
BinaryNumber(4, 102, "01100110")
150+
)
151+
binaryCollection.insertMany(binaryNumbers)
152+
// :remove-end:
153+
154+
val bitmask = 34.toLong() // 00100010 in binary
155+
val bitsComparison = Filters.bitsAllSet(BinaryNumber::decimalValue.name, bitmask)
156+
val resultsFlow = binaryCollection.find(bitsComparison)
157+
resultsFlow.collect { println(it) }
158+
// :snippet-end:
159+
val expected = listOf(
160+
BinaryNumber(1, 54, "00110110"),
161+
BinaryNumber(4, 102, "01100110")
162+
)
163+
assertEquals(expected, resultsFlow.toList())
164+
}
165+
166+
@Test
167+
fun geoWithinComparisonTest() = runBlocking {
168+
// :snippet-start: geoWithinComparison
169+
data class Store(
170+
@BsonId val id: Int,
171+
val name: String,
172+
val coordinates: Point
173+
)
174+
val collection = database.getCollection<Store>("stores")
175+
// :remove-start:
176+
val stores = listOf(
177+
Store(
178+
13,
179+
"Store 13",
180+
Point(Position(2.0, 2.0))
181+
),
182+
Store(
183+
14,
184+
"Store 14",
185+
Point(Position(5.0, 6.0))
186+
),
187+
Store(
188+
15,
189+
"Store 15",
190+
Point(Position(1.0, 3.0))
191+
),
192+
Store(
193+
16,
194+
"Store 16",
195+
Point(Position(4.0, 7.0))
196+
)
197+
)
198+
collection.insertMany(stores)
199+
// :remove-end:
200+
201+
val square = Polygon(listOf(
202+
Position(0.0, 0.0),
203+
Position(4.0, 0.0),
204+
Position(4.0, 4.0),
205+
Position(0.0, 4.0),
206+
Position(0.0, 0.0)))
207+
val geoWithinComparison = Filters.geoWithin(Store::coordinates.name, square)
208+
209+
val resultsFlow = collection.find(geoWithinComparison)
210+
resultsFlow.collect { println(it) }
211+
// :snippet-end:
212+
assertEquals(2, resultsFlow.toList().size)
213+
assertEquals("Store 13", resultsFlow.firstOrNull()?.name)
214+
}
215+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
import com.mongodb.client.model.Indexes
3+
import com.mongodb.kotlin.client.coroutine.MongoClient
4+
import config.getConfig
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
import org.junit.jupiter.api.AfterAll
8+
import org.junit.jupiter.api.Test
9+
import kotlin.test.assertEquals
10+
11+
class IndexesBuildersTest {
12+
13+
companion object {
14+
val config = getConfig()
15+
val client = MongoClient.create(config.connectionUri)
16+
val database = client.getDatabase("marketing")
17+
val collection = database.getCollection<Document>("users")
18+
19+
@AfterAll
20+
@JvmStatic
21+
fun afterAll() {
22+
runBlocking {
23+
database.drop()
24+
client.close()
25+
}
26+
}
27+
}
28+
29+
@Test
30+
fun ascendingIndexTest() = runBlocking {
31+
// :snippet-start: ascending-index
32+
val ascendingIndex = Indexes.ascending("name")
33+
val indexName = collection.createIndex(ascendingIndex)
34+
println(indexName)
35+
// :snippet-end:
36+
assertEquals("name_1", indexName)
37+
}
38+
39+
@Test
40+
fun descendingIndexTest() = runBlocking {
41+
// :snippet-start: descending-index
42+
val descendingIndex = Indexes.descending("capacity")
43+
val indexName = collection.createIndex(descendingIndex)
44+
println(indexName)
45+
// :snippet-end:
46+
assertEquals("capacity_-1", indexName)
47+
}
48+
49+
@Test
50+
fun compoundIndexExampleTest() = runBlocking {
51+
// :snippet-start: compound-index
52+
val compoundIndexExample = Indexes.compoundIndex(
53+
Indexes.descending("capacity", "year"),
54+
Indexes.ascending("name")
55+
)
56+
val indexName = collection.createIndex(compoundIndexExample)
57+
println(indexName)
58+
// :snippet-end:
59+
assertEquals("capacity_-1_year_-1_name_1", indexName)
60+
}
61+
62+
@Test
63+
fun textIndexTest() = runBlocking {
64+
// :snippet-start: text-index
65+
val textIndex = Indexes.text("theaters")
66+
val indexName = collection.createIndex(textIndex)
67+
println(indexName)
68+
// :snippet-end:
69+
assertEquals("theaters_text", indexName)
70+
}
71+
72+
@Test
73+
fun hashedIndexTest() = runBlocking {
74+
// :snippet-start: hashed-index
75+
val hashedIndex = Indexes.hashed("capacity")
76+
val indexName = collection.createIndex(hashedIndex)
77+
println(indexName)
78+
// :snippet-end:
79+
assertEquals("capacity_hashed", indexName)
80+
}
81+
82+
@Test
83+
fun geo2dsphereIndexTest() = runBlocking {
84+
// :snippet-start: geo2dsphere-index
85+
val geo2dsphereIndex = Indexes.geo2dsphere("location")
86+
val indexName = collection.createIndex(geo2dsphereIndex)
87+
println(indexName)
88+
// :snippet-end:
89+
assertEquals("location_2dsphere", indexName)
90+
}
91+
}

0 commit comments

Comments
 (0)