【问题标题】:Declare array of bsonType in mongoDb schema在 mongoDb 模式中声明 bsonType 数组
【发布时间】:2021-08-09 13:08:27
【问题描述】:

如何在 mongoDb 模式中声明不同类型的数组?

我在文档中有一个值,它可以是 double 或 int,我尝试像这样声明它:

"numberOf": {
                "bsonType": ["long", "int"]
              },

我收到了那个错误:

属性“numberOf”的类型无效:不支持类型 [long,int]

doc 中,他们说您可以声明一个 bsonTypes 数组或类似的类型:

"type": "<JSON Type>" | ["<JSON Type>", ...],
"bsonType": "<BSON Type>" | ["<BSON Type>", ...],

我也试过了:

"numberOf": {
                "type": "number"
              },

我无法保存我的架构得到这个:

我不知道我错过了什么。

【问题讨论】:

  • 我不知道这是什么 - ["long", "int"]。如果要定义多种类型,请使用"bsonType": "int" | "long" | "double" | "decimal",
  • 我试过了,还是不行
  • “它不起作用”相当模糊。你能澄清你尝试了什么,什么不起作用吗?请更新问题的详细信息。
  • 我只想在我的属性上有多个 bsonTypes。应该很容易。例如,“numberOf”可以是 int 或 double。

标签: json mongodb realm bson


【解决方案1】:

显然问题来自同步see here。 他们正在努力。

目前,无法为单个字段同步多种类型的数据。

我所做的是将我的类型更改为“混合”:

"numberOf": {
                "bsonType": "mixed"
              },

此功能处于测试阶段 (see here),您可能需要更新您的 Realm 包。

在你的终端里做

npm install realm

然后

cd ios
pod install

小心破坏性更改。

如果需要,然后在您的模拟器/模拟器上卸载您的应用程序

npx react-native run-ios

npx react-native run-android

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多