【问题标题】:Why Mongoose cant create an index in MongoDB Atlas?为什么 Mongoose 不能在 MongoDB Atlas 中创建索引?
【发布时间】:2019-03-10 22:12:59
【问题描述】:

我有一个 Mongoose 架构,其中包含一个具有特定索引的字段:

const reportSchema = new mongoose.Schema({
    coords: {
    type: [Number],
    required: true,
    index: '2dsphere'
    },
…
}

它在我的本地机器上运行良好,所以当我通过 shell 连接到 MongoDB 时,我会得到 db.reports.getIndexes() 的输出:

[
{
    "v" : 2,
    "key" : {
        "_id" : 1
    },
    "name" : "_id_",
    "ns" : "weatherApp.reports"
},
{
    "v" : 2,
    "key" : {
        "coords" : "2dsphere"
    },
    "name" : "coords_2dsphere",
    "ns" : "weatherApp.reports",
    "background" : true,
    "2dsphereIndexVersion" : 3
}
]

然后我将此应用程序部署到 Heroku 并连接到 MongoDB Atlas 而不是我的本地数据库。它适用于保存和检索数据,但没有创建索引(仅默认一个):

[
{
    "v" : 2,
    "key" : {
        "_id" : 1
    },
    "name" : "_id_",
    "ns" : "weatherApp.reports"
}
]

什么可能导致这个问题? Atlas 允许通过 Web GUI 创建索引,它可以很好地工作以及从 shell 中创建索引。但由于某种原因,Mongoose 无法执行此操作。

【问题讨论】:

  • 您可以查看地图集日志吗?此外,您自动迁移您的数据?插入数据后索引仍然不存在?
  • @anouarKacem,不,插入数据后仍然没有索引。试图检查 Atlas 上的日志,但实际上无法弄清楚在哪里可以访问它们。
  • @MishaGoncharov 你有没有发现问题是什么?
  • @Joseph,不幸的是,没有。阿特拉斯支持也没有帮助:-(
  • @MishaGoncharov 我刚刚弄清楚我出了什么问题。我会提交一个答案。

标签: mongodb mongoose mongodb-atlas


【解决方案1】:

我在使用猫鼬版v5.0.16 时遇到了同样的问题。但是,自从我更新到 v5.3.6 后,它现在正在为我在 Mongo Atlas 上创建(复合)索引。 (我刚刚编写了一个包含两个版本的示例应用程序来验证情况是否如此)。

我不确定哪个版本解决了这个问题,但它介于 v5.0.16v5.3.6 之间,v5.3.6 正在工作。

【讨论】:

  • 哇,我用的是 5.2.13。更新一下看看,谢谢
  • 是的,它有帮助!
  • @MishaGoncharov 很高兴听到这个消息!
猜你喜欢
  • 2021-10-25
  • 2018-09-01
  • 2020-03-22
  • 2012-09-16
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多