我如何使用 mongodb 与电子?

How do i use mongodb with electron?

我目前正在使用 Electron 和 MongoDB 构建桌面应用程序。本应用的objective是在应用的本地范围内(而不是在服务器上)收集和存储各种客户的信息。我用 node.js 对 MongoDB 做了一些研究;但是我还没有找到在 Electron 中使用它的方法。

这是一个用于MongoDB管理的electron应用程序,您可以查看代码作为示例,了解如何使用mongodb和electron。

https://github.com/officert/mongotron

基本上您可以像通常在 node.js in the Main process and then communicate with Renderer process through the ipc module 中使用的那样使用 mongodb。

例如:

渲染进程

<html>
  <head></head>
  <body>
    <script>
    const ipc = require('electron').ipcRenderer;
    const informationBtn = document.getElementById('information-dialog')

    informationBtn.addEventListener('click', function (event) {
      ipc.send('create-user')
    })
    </script>
  </body>
<html>

主要流程

const ipc = require('electron').ipcMain
const mongo = require('some-mongo-module')

ipc.on('create-user', function (event) {
  /* MONGODB CODE */
})

我建议您使用可以在 http://electron.atom.io/

中找到的入门应用程序

我正在使用 Electron + React + Typescript,我不得不像这样导入猫鼬:

const mongoose = window.require("mongoose");
// then you can connect to it like so:
mongoose.connect("mongodb://localhost:27017/test");

这将消除错误:

TypeError: mongoose.connect is not a function

您可以使用 mongodb 领域,它具有与 mongo 驱动程序

相似的 MQL API

https://docs.mongodb.com/realm/sdk/node/integrations/electron-cra/