gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:57:51 36 4
gpt4 key购买 nike

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

连接功能已更新,如本answer所述。但将 npm 版本升级到 3.4.1 后,即使文档在集合中可用,findOne 查询也会返回 null 值。这是与 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和mongoDB Atlas中持有数据库的结构不同。 mlab共享集群代表一个数据库,而mongoDB atlas共享集群可以包含多个数据库。

下图显示了 mlab 数据库。

Databases in mlab

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

database

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

Atlas集群 atlas cluster

集群内的数据库 atlas database

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

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

关于node.js - MongoDB 查询返回 null,即使它在从 mlab 迁移到 mongoDB atlas 后在集合中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61122728/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com