![](/img/trans.png)
[英]No data received in Mongodb Atlas collection, even though connection is established with app
[英]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 地圖集數據庫版本是4.2.5
。
如this answer中所述,連接 function 已更新。 但是在將 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.databaseName
和db.listCollections().toArray()
對其進行了測試。 isconnected
返回true
,返回的數據庫名也是正確的。 但是db.listCollections().toArray()
返回了一個空數組,這意味着我的數據庫中沒有 collections 是不可能的。
然后我嘗試了一個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
)。 我也使用 web 瀏覽器檢查了 MongoDB Atlas 中的相關文檔,並且未通過將"models"
值添加到unsubscribedEmails
數組來更新文檔。
除此之外,我還嘗試通過刪除package-lock.json
來全新安裝node_modules
。
由於我從 mlab 遷移數據庫,是否有可能超出MongoDB 共享集群的限制而發生此問題。
很高興聽到有關此問題的建議
在 mlab 和 mongoDB Atlas 中保存數據庫的結構不同。 mlab 共享集群代表一個數據庫,而 mongoDB atlas 共享集群可以包含多個數據庫。
下圖顯示了 mlab 數據庫。
這是您在數據庫中使用 go 時的圖像
遷移過程后(使用 mlab 和 Atlas 提供的工具進行遷移)。 它創建了一個名為maturify-demo
的共享集群和一個名為maturify_demo
的數據庫。 看看下面的圖片。
在遷移過程中,它更改了 Mlab 中使用的集群名稱( maturify_demo
到maturify-demo
)
使用客戶端連接到數據庫時,我使用maturify-demo
作為 Db 名稱,認為集群將數據庫表示為 Mlab ( cachedDb = client.db('maturify-demo');
)。 實際上必須是maturify_demo
。 但是當我使用db.serverConfig.isConnected()
和db.databaseName
測試數據庫連接時。 它返回了true
和maturify-demo
,這有點令人困惑,它顯示了 MongoDB Atlas 中不可用的數據庫。 正如@Joe 在下面的評論中提到的那樣,即使該數據庫當前不存在,它也允許將文檔添加為新數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.