簡體   English   中英

MongoDb 連接初始化和查詢很慢,而使用 Electron

[英]MongoDb connection initialization and query is very slow, while using Electron

首先,我必須說我是 Electron 的新手。 而且我認為問題出在 Electron 上,因為沒有 Electron (或使用 Electron,如果第一個 ZFC35FDC70D5FC69D526 頁面使用了),相同的代碼運行得非常快。 如果您能幫助我在案例 2 中更快地訪問數據庫,我將不勝感激。(附加信息,軟件版本來自 package.json ==> "electron": "^9.0.5" & "mongodb": "^3.5 .9" & windows 10)

案例 1.它運行正常(這意味着,非常快,以毫秒為單位)

說明:在 Electron 中使用firstPage.html (渲染器進程)實例化 BrowserWindow 並在其 javascript 代碼中調用以下函數后,

  • client.connect(...) ==> 幾毫秒
  • 查詢數據庫,例如 db.find() ==> 幾毫秒

案例 2.非常慢(以秒為單位)

Description: I put an " a tag " (see <a href="secondPage.html"...>) in this first BrowserWindow's HTML page ( firstPage.html ) and click to this link to see another HTML page ( secondPage.html )。 如果我單擊 secondPage.html 中的按鈕,我意識到數據庫訪問變得非常緩慢。

  • client.connect(...) ==> 大約 10 秒
  • 查詢數據庫,例如 db.find() ==> 超過 10 秒

以下是相關代碼:

    firstPage.html:
        <a id="idInFirstPage" class="button-link">Get Data</a> <!-- very fast db access -->
        <a href="secondPage.html">Change Page</a>
        <script src="firstPage.js"></script>

    secondPage.html:
        <a id="idInSecondPage" class="button-link">Get Data</a> <!-- very slow db access -->
        <script src="secondPage.js"></script>

    firstPage.js (the only difference to secondPage.js is the id in querySelector):
        // I searched in internet for similar problems and optimized this url connection string, 
        // i.e. i changed localhost with 127.0.0.1 and added family, but problem still persists

        const url = "mongodb://127.0.0.1:27017&family=4"; 
        const client = new MongoClient(url, { useUnifiedTopology: true,
                                          useNewUrlParser: true });
        const dbName = "myDatabase";

        // only change the #idInFirstPage to #idInSecondPage in secondPage.js
        document.querySelector("#idInFirstPage").addEventListener("click", () => 
             {
                 client.connect(function (err) {
                     assert.equal(null, err);
                     console.log("Connected successfully to server");
                     try {
                           const db = client.db(dbName);

                           findDocuments(db, () => 
                           {
                              client.close();
                           });
                     } catch (e) {
                          console.log("Something gone wrong ==> " + e);
                          client.close();
                     }
                  });
               });

        var findDocuments = function (db, callback) {
            // Get the documents collection
            var collection = db.collection("myDataProducts");
            // Find some documents
            collection.find({}).toArray((err, docs) => {
                assert.equal(err, null);

                if (typeof docs !== "undefined" && docs.length > 0) {
                    // the array is defined and has at least one element
                    console.log("Found the following documents");
                    console.log(docs);
                }
                else {
                    console.log("No element in array!");
                }
                callback(docs);
            });
        };

您每次都在創建和連接數據庫客戶端實例。 在這種情況下,您將在第一頁上創建一個實例,然后在第二頁上再次創建一個實例。

我建議你在你的主進程中進行數據庫操作,這樣你只需要一個實例,渲染的進程就不會面臨繁重的任務。 實際上,渲染器是用於在 chromium-browser 上渲染組件。

所以你可以像這樣使用。

main.js

...
var MongoClient = require("mongodb").MongoClient;
var assert = require("assert");

const url = "mongodb://127.0.0.1:27017&family=4";
const client = new MongoClient(url, {
  useUnifiedTopology: true,
  useNewUrlParser: true,
});
const dbName = "yourDBName";

const readData = async () => {
     ... // you don't need to close your client instance
}

ipcMain.on('readData', async (event, arg) => {
    const data = await readData();
    event.reply('data', data);
})

...

renderer.js (你的情況,firstPage 和 secondPage)

...
document.querySelector("#idInSecondPage").addEventListener("click", () => {
     ipcRenderer.send('readData', {dbName: 'products'} )
});
ipcRenderer.on('data', data => {console.log(data} )
...

暫無
暫無

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

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