"Invalid BSON field name" upsert 期间

"Invalid BSON field name" during upsert

我正在尝试使用以下代码将数据更新到 Mongo 集合中:

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)

执行更新插入时(上面代码的最后三行),我得到 异常 java.lang.IllegalArgumentException: Invalid BSON field name AuthProvider.

我该如何解决这个问题?

我没有发现类似问题的答案中提到的字段 AuthProvider(没有空格,没有点等特殊字符)有任何问题。

正在替换

return UpdateOneModel<Document>(
            query,
            update,
            Upsert
    )

来自

return ReplaceOneModel<Document>(query, Document(data), Upsert)

解决了问题。