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

问题描述 投票:3回答:1

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

连接功能已按照此answer中所述进行了更新。但是将npm版本升级到3.4.1之后,即使文档在集合中可用,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);
    });

因此,我想知道数据库连接是否存在问题,因此我在运行db.serverConfig.isConnected()db.databaseNamedb.listCollections().toArray()时进行了测试。 isconnected返回了true,并且返回的数据库名称也正确。但是db.listCollections().toArray()返回了一个空数组,这意味着我的数据库中没有不能收集的集合。

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

db.collection('users').findOneAndUpdate(
        { emails: { $elemMatch: { email: "[email protected]" } } },
        { $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)。我也使用Web浏览器在MongoDB Atlas中检查了相关文档,并且未通过将"models"值添加到unsubscribedEmails数组来更新该文档。

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

自从我从mlab迁移数据库以来,超过limits of MongoDB shared cluster的可能性就可能出现此问题。

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

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

node.js mongodb migration mlab mongodb-atlas
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.