I am try to set up an environment using Node.js and MongoDB on an installation of ubuntu. I am able to start a mongo database shell by running the mongo command. But I am unsure of how to accomplish a couple things.
- How do I set up MongoDB to run persistently, so that I can connect to it?
- How do I connect to a certain MongoDB database from within Node.js once it is up and running?
I have the mongoose package installed, but I am still confused on how to connect to a particular database with it.
Depends on how you installed the MongoDB.
If you used the whole
apt-getinstallation process, it generally installs aninit.dscript and the whole thing just runs with the default settings.Try a
ps -ef | grep mongodto look for themongodprocess.If you “installed” by downloading the
tarfile, then take a look at usingupstartor similar process for making it run persistently. You’ll want to use config files.Based on the Mongoose page, here is the basic connection process:
The standard connection string format is detailed here. Note that in my experience, not all drivers support all of the connection string features equally well, so do test your configuration.