I am using Scala 2.12 with the following Maven coordinates:
- org.mongodb.scala:mongo-scala-driver_2.12:4.6.0
- org.mongodb.scala:mongo-scala-bson_2.12:4.6.0
- org.mongodb:bson:4.6.0
I am following the MongoDB Scala Driver quick tour guide here:
http://mongodb.github.io/mongo-scala-driver/2.9/getting-started/quick-tour/
I am successfully in making client connectivity to the Mongo server and select the collection without any issue.
I am having trouble querying the collection and the goal of obtain the resulting document collections in the form of Seq[JSON].
I am referencing and using the example Mongo (scala) code from here:
Here is my example code:
val exStartDt = LocalDate.of(2022,4,26) // exStartDt: java.time.LocalDate = 2022-04-26
val exEndDt = LocalDate.of(2022,4,27) // exEndDt: java.time.LocalDate = 2022-04-27
val exStartDateMilli = java.util.Date.from(exStartDt.atStartOfDay().atZone(ZoneId.of("UTC")).toInstant()).getTime() // exStartDateMilli: Long = 1650931200000
val exEndDateMilli = java.util.Date.from(exEndDt.atStartOfDay().atZone(ZoneId.of("UTC")).toInstant()).getTime() // exEndDateMilli: Long = 1651017600000
val exBdtStart = BsonDateTime(exStartDateMilli) // exBdtStart: org.mongodb.scala.bson.BsonDateTime = BsonDateTime{value=1650931200000}
val exBdtEnd = BsonDateTime(exEndDateMilli) // exBdtEnd: org.mongodb.scala.bson.BsonDateTime = BsonDateTime{value=1651017600000}
val mQuery = BsonDocument("createdDateTime" -> BsonDocument("$gte" -> exBdtStart, "$lt" -> exBdtEnd),
"_id" -> BsonDocument("$eq" -> "ABC1234567890"))
// mQuery: org.mongodb.scala.bson.BsonDocument = {"createdDateTime": {"$gte": {"$date": "2022-04-26T00:00:00Z"}, "$lt": {"$date": "2022-04-27T00:00:00Z"}}, "_id": {"$eq": "ABC1234567890"}}
val collection: MongoCollection[Document] = db.getCollection(myCollection)
// collection: org.mongodb.scala.MongoCollection[org.mongodb.scala.bson.Document] = MongoCollection(com.mongodb.reactivestreams.client.internal.MongoCollectionImpl@123456789)
var findObservable = collection.find(mQuery)
// mResult: org.mongodb.scala.FindObservable[org.mongodb.scala.bson.Document] = FindObservable(com.mongodb.reactivestreams.client.internal.FindPublisherImpl@123456789)
println(findObservable.execute().size)
// error: value execute is not a member of org.mongodb.scala.FindObservable[org.mongodb.scala.bson.Document]
// println(findObservable.execute().size)
// ^
It is complaining that execute()
is not a member of org.mongodb.scala.FindObservable[org.mongodb.scala.bson.Document]
.
None of the Scala Mongo client query code (that I can find) is current consistent and working for this basic query example.
I am current stuck and I could not find working examples that is current as of April 2022.
I appreciate any pointers or better references that can help me to accomplish my objectives.
Thanks!