I use the official mongo spark connector.
On my database i have one admin with root role, so he has all right.
i have created a config as follows :
val readConfig = ReadConfig(Map("spark.mongodb.auth.uri" -> "mongodb://<userName>:<password>@<ip>:27017/admin",
"spark.mongodb.input.uri" -> "mongodb://<ip>:27017/MyDatabase.myCollection"))
but when i try to read some data i get an error "not authorized to execute command."
i don't understand why my root user is not authorized.
It's because "spark.mongodb.auth.uri"
is not a configuration setting.
As the input uri doesn't have the authentication parameters the read is not authorised.
Try:
val readConfig = ReadConfig(Map(
"uri" -> "mongodb://<userName>:<password>@<ip>:27017/myDatabase.myCollection?authSource=admin"))
or:
val readConfig = ReadConfig(Map(
"uri" -> "mongodb://<userName>:<password>@<ip>:27017", // uses the default db to auth against (admin)
"database" -> "myDatabase",
"collection" -> "myCollection"))
To avoid a full scan, you can do as below:
val rdd = MongoSpark.load(sc)
val aggregatedRdd = rdd.withPipeline(Seq(Document.parse("{ $match: { test : { $gt : 5 } } }")))
println(aggregatedRdd.count)
println(aggregatedRdd.first.toJson)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With