【问题标题】:Does MongoDB store any information about document attribute types?MongoDB 是否存储有关文档属性类型的任何信息?
【发布时间】:2017-04-11 15:20:17
【问题描述】:

当我尝试向集合添加索引时
<collection>.createIndex({ reaction: 1, team_id: 1 }, { unique: true })
我收到错误 10088:无法索引并行数组。

但我的集合中没有任何数组。

当我使用 {$type: 'array'} 搜索属性时,我显示零个文档。
当我搜索 {$type: 'string'} 时,所有文档都会返回。

以前这些属性是数组,但后来被转换了。
mongodb 是否在某处存储属性类型信息?

// Original document
{
  "value" : 5,
  "team_id" : ["T024F579X"],
  "reaction" : ["star2"],
}

// new converted document
{
  "value" : 5,
  "team_id" : "T024F579X",
  "reaction" : "star2",
}

mongod version: 3.2.11 (MMAPv1)
_id 是此集合中的唯一索引

【问题讨论】:

  • 请说明您何时/何地/如何收到该错误。
  • 更新了!感谢您的反馈
  • 您能否提供您的数据样本?
  • 哪个 MongoDB 版本?
  • 3.2.11是版本

标签: mongodb indexing types


【解决方案1】:

我认为这里的答案是:不,mongo 不存储类型信息。

原来我确实有一个包含数组属性的文档。
问题在于$type 的工作原理。
"team_id" : ["T024F579X"], 中的 team_id 将被 mongo 读取为类型 string。

from the mongo docs

当应用于数组时,$type 匹配任何内部元素

意味着只有嵌套数组将被读取为类型array。

查询集合中任何现有top level 数组的有效方法是使用$size Mongo $size docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多