MongoDB 查询返回 null,即使它在从 mlab 迁移到 mongoDB atlas 后在集合中可用

Raj*_*ana 5 migration mongodb node.js mlab mongodb-atlas

我正在将数据库从 Mlab 迁移到 MongoDB Atlas。我们必须将 mongodb 的 npm 版本升级到3.4.1MongoDB atlas 数据库版本4.2.5

连接功能已按照此答案中所述进行了更新。但是在将 npm 版本升级到3.4.1findOne 后,即使文档在集合中可用,findOne 查询也会返回空值。这是与 findOne 查询相关的代码部分,

  db.collection('organisations').findOne({ _id: database.ObjectID(orgState) })
    .then((activeOrganisation) => {
      console.log(activeOrganisation);
      data.activeOrganisation = activeOrganisation;
      callback(null, activeOrganisation);
    }, (error) => {
      callback(error, null);
    });
Run Code Online (Sandbox Code Playgroud)

因此我想知道数据库连接是否有问题,因此我使用运行db.serverConfig.isConnected(),db.databaseName和进行了测试db.listCollections().toArray()。返回isconnectedtrue和返回的数据库名也是正确的。但db.listCollections().toArray()返回一个空数组,这意味着我的数据库中没有不能的集合。

然后我尝试findOneAndUpdate查询只是为了检查会发生什么。这是它的相关代码,

db.collection('users').findOneAndUpdate(
        { emails: { $elemMatch: { email: "rajitha1591@outlook.com" } } },
        { $addToSet: { unsubscribedEmails: "models" } })
        .then((result) => {
          console.log(result);
    
            if (!result) {
                console.error('Error: ', 'User not found')
            }
            console.log('Output: ', 'Sucessfully unsubscribed');
            callback(null,'Successful')
        }, (error) => {
            callback(error, null);
        });
Run Code Online (Sandbox Code Playgroud)

结果包含,

{
  lastErrorObject: { n: 0, updatedExisting: false },
  value: null,
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1586436331 },
    signature: { hash: [Binary], keyId: [Long] }
  },
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1586436331 }
}
Run Code Online (Sandbox Code Playgroud)

这清楚地表明该文档没有更新(updatedExisting: false)。我也使用网络浏览器检查了 MongoDB Atlas 中的相关文档,但该文档并未通过将"models"值添加到unsubscribedEmails数组来更新。

除此之外,我还尝试node_modules通过删除来重新安装。package-lock.json

由于我从 mlab 迁移数据库,是否有可能超出MongoDB 共享集群的限制 而出现此问题。

很高兴听到有关此问题的建议

Raj*_*ana 0

mlab和mongoDB Atlas中持有数据库的结构不同。mlab共享集群代表一个数据库,而mongoDB atlas共享集群可以包含多个数据库。

下图显示了 mlab 数据库。

mlab 中的数据库

这是进入数据库时​​的图像

数据库

迁移过程之后(使用mlab和Atlas提供的工具迁移)。它创建了一个名为 的共享集群maturify-demo和一个名为 的数据库maturify_demo。看看下面的图片。

阿特拉斯星团 阿特拉斯星团

集群内的数据库 阿特拉斯数据库

在迁移过程中,它更改了 Mlab 中使用的集群名称(maturify_demo更改为maturify-demo

当使用客户端连接到数据库时,我使用maturify-demoDb 名称,认为集群将数据库表示为 Mlab ( cachedDb = client.db('maturify-demo');)。实际上必须如此maturify_demodb.serverConfig.isConnected()但是当我使用和测试数据库连接时db.databaseName。它返回了truematurify-demo这有点令人困惑,它显示了 MongoDB Atlas 中不可用的数据库。正如@Joe 在下面的评论中提到的,它允许将文档添加为新数据库,即使该数据库当前不存在。