簡體   English   中英

MongoDB 查詢返回 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 地圖集數據庫版本是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.databaseNamedb.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 數據庫。

mlab 中的數據庫

這是您在數據庫中使用 go 時的圖像

數據庫

遷移過程后(使用 mlab 和 Atlas 提供的工具進行遷移)。 它創建了一個名為maturify-demo的共享集群和一個名為maturify_demo的數據庫。 看看下面的圖片。

阿特拉斯集群圖集集群

集群內的數據庫地圖集數據庫

在遷移過程中,它更改了 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 在下面的評論中提到的那樣,即使該數據庫當前不存在,它也允許將文檔添加為新數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM