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

MongoDB query returns null even though it is available in the collection after migrating from mlab to mongoDB atlas

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

连接功能已更新,如所述。但是在将 npm 版本升级到 3.4.1 之后,findOne 查询 returns 一个空值,即使该文档在集合中可用。这是与 findOne 查询相关的代码部分,

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

因此我想知道是不是数据库连接有问题所以我用运行 db.serverConfig.isConnected() , db.databaseNamedb.listCollections().toArray() 测试了它。 isconnected返回true,返回的数据库名也是正确的。但是 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);
        });

结果包含,

{
  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 }
}

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

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

因为我从mlab迁移了数据库,是否有可能超过limits of MongoDB shared cluster出现这个问题。

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

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

下图显示了 mlab 数据库。

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

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

Atlas 集群

集群内的数据库

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

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