Reputation: 18720
I'm trying to upsert data into a Mongo collection using the following code:
val UsersColl = "Users"
val UsersColl_AuthProvider = "AuthProvider"
val UsersColl_UserId = "UserId"
val UsersColl_Active = "Active"
val UsersColl_SlackRealName = "SlackRealName"
val UsersColl_SlackTeamId = "SlackTeamId"
val AuthProvider_Slack = "Slack"
val Upsert = UpdateOptions().upsert(true)
internal open fun slackUserToUpsertStatement(usr: SlackUserData): WriteModel<Document> {
val query = BasicDBObject(UsersColl_UserId, usr.id())
val data = mapOf(
UsersColl_AuthProvider to AuthProvider_Slack,
UsersColl_UserId to usr.id(),
UsersColl_Active to true,
UsersColl_SlackRealName to usr.realName,
UsersColl_SlackTeamId to usr.teamId
)
val update = BasicDBObject(data)
return UpdateOneModel<Document>(
query,
update,
Upsert
)
}
val updates = users.map { slackUserToUpsertStatement(it) }.toList()
val coll = db.getCollection(UsersColl)
coll.bulkWrite(updates)
When the upsert is executed (last three lines in the above code), I get the
exception java.lang.IllegalArgumentException: Invalid BSON field name AuthProvider
.
How can I fix this?
I don't see any problem with the field AuthProvider
(no spaces, no special characters like dots) mentioned in answers to similar questions.
Upvotes: 3
Views: 3678
Reputation: 18720
Replacing
return UpdateOneModel<Document>(
query,
update,
Upsert
)
by
return ReplaceOneModel<Document>(query, Document(data), Upsert)
fixed the problem.
Upvotes: 2