我想在我的应用程序中使用 Mongo DB 的 Atlas 设备同步功能。我在我的 Kotlin 代码中建模了一个 BankAccount 类,如下所示。
open class BankAccount: RealmObject, Searchable {
@PrimaryKey
var _id: ObjectId = ObjectId()
var accountHolderName: String = ""
var accountNumber: String = ""
var accountType: String = ""
var bankName: String = ""
var cardCvv: String = ""
var cardNo: String = ""
var cardPin: String = ""
var expiryDate: String = ""
var haveBankingApp: Boolean = false
var haveCard: Boolean = false
var haveUpi: Boolean = false
var ifscCode: String = ""
var isArchived: Int = 0
var loginPin: String = ""
var nameOnCard: String = ""
var ownerId: String = ""
var regEmail: String = ""
var regMobNo: String = ""
var remarks: String = ""
var transactionPin: String = ""
var upiPin: String = ""
override fun doesMatchSearchQuery(query: String): Boolean {
return bankName.contains(query.trim(), ignoreCase = true) || accountHolderName.contains(query.trim(), ignoreCase = true)
}
}
我的 JSON 架构如下所示:
{
"title": "BankAccount",
"properties": {
"_id": {
"bsonType": "objectId"
},
"ownerId": {
"bsonType": "string"
},
"bankName": {
"bsonType": "string"
},
"accountHolderName": {
"bsonType": "string"
},
"accountType": {
"bsonType": "string"
},
"accountNumber": {
"bsonType": "string"
},
"ifscCode": {
"bsonType": "string"
},
"regMobNo": {
"bsonType": "string"
},
"regEmail": {
"bsonType": "string"
},
"remarks": {
"bsonType": "string"
},
"haveCard": {
"bsonType": "bool"
},
"nameOnCard": {
"bsonType": "string"
},
"cardNo": {
"bsonType": "string"
},
"expiryDate": {
"bsonType": "string"
},
"cardCvv": {
"bsonType": "string"
},
"cardPin": {
"bsonType": "string"
},
"haveUpi": {
"bsonType": "bool"
},
"upiPin": {
"bsonType": "string"
},
"haveBankingApp": {
"bsonType": "bool"
},
"loginPin": {
"bsonType": "string"
},
"transactionPin": {
"bsonType": "string"
},
"isArchived": {
"bsonType": "int"
}
},
"required": [
"_id",
"ownerId",
"bankName",
"accountHolderName",
"accountType",
"accountNumber",
"ifscCode",
"regMobNo",
"regEmail",
"remarks",
"haveCard",
"nameOnCard",
"cardNo",
"expiryDate",
"cardCvv",
"cardPin",
"haveUpi",
"upiPin",
"haveBankingApp",
"loginPin",
"transactionPin",
"isArchived"
]
}
一切都很完美。我已启用设备同步,以正确的方式正确设置权限,并完美配置 Mongo DB 领域,但我仍然不断收到主键的此错误:(
抑制:kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{取消}@5b935b7,Dispatchers.Main.immediate] 引起原因:java.lang.IllegalStateException: [RLM_ERR_SCHEMA_VALIDATION_FAILED]:架构验证失败,原因是 出现以下错误: - 同步 Realm 上必须有一个名为“_id”的主键属性,但未找到“BankAccount”类型的主键属性
您能找到导致问题的原因吗?