23

I'm currently building a desktop application using Electron and MongoDB. The objective of this application is to collect and store information of various customers in the local scope of the application (not on a server). I've done some research into MongoDB with node.js; however I haven't found a way to use it in Electron.

1

3 Answers 3

16

This is an electron app for MongoDB management, you can check the code as an example on how to use mongodb and electron.

https://github.com/officert/mongotron

Basically you can use mongodb as you would normally use in node.js in the Main process and then communicate with Renderer process through the ipc module.

For example:

Renderer process

<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>

Main process

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

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

I would recommend you to use the get started app that you can find in http://electron.atom.io/

3
  • 52
    can MongoDB be packaged in the Electron app or it will require a separate installation on the client machine?
    – user449689
    Nov 21, 2016 at 10:19
  • 1
    @user449689 do you find the answer for the above question of separate installation?
    – manny
    Dec 27, 2017 at 8:04
  • 1
    I couldn't find a way to package it and also there are license implications of packaging it as well. The approach I'm taking though is to use node to download msi (for Windows) and tgz for Mac. You can then use node to run msiexec.exe for Windows and on Mac just extract folder.
    – jth_92
    Feb 25, 2018 at 3:25
0

I am using Electron + React + Typescript and I had to import mongoose like so:

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

This will get rid of the error:

TypeError: mongoose.connect is not a function
0

you can use mongodb realm which has a similar MQL API as the mongo driver

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

1
  • Realm has a serious limitation with the Node SDK. It can only be used on the main thread, so if there is even the slightest chance you will need to turn to worker threads to remove bottlenecks in data processing, you are out of luck. In GitHub issues about this, the Realm group has expressed no interest in supporting worker threads, also, which to me is almost coming out and saying they aren't really supporting Node and don't care about it. Mar 6 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.