Point mongoos.connect at localhost mongodb - javascript

I am studying the MEAN stack by using this tutorial. But the tutorial connects to a remote mongodb installation. I have MongoDB running in the CentOS7 localhost.
How do I alter the mongoose connect line in server.js from the tutorial link above to connect to a localhost database instead of a remote database?
Here is the current line from server.js that needs to be changed to point to the localhost mongodb:
mongoose.connect('mongodb://node:nodeuser#mongo.onmodulus.net:27017/uwO3mypu');
A specific mongodb database has not been created yet. Do I need to create that also?

I'm fairly new at Mongo to, but I know how to connect to a local db. Basically I had to do the following:
Download the latest version of mongodb from https://www.mongodb.com/download-center?jmp=nav#community (according to your settings)
Once installed, I've created a folder that will be containing my DB.
Use a command line instance to start mongo like this:
mongod --dbpath [YOUR_DB_PATH]
Created a DB use mydb
With that you should have already a mongodb db instance looking for connections on default port. So you can change that line for this:
mongoose.connect('mongodb://localhost:27017/mydb');
Again, this is really basic and it is creating a mongo DB connection with all default options. So this will keep you rolling, but you may need to dig a bit more for custom options.
Hope this helps

Related

How can I set database authentication in Mongodb - Is SSH Pipes better for MongoDB?

I have installed MongoDB in my computer. I did't define any password and username for the installation. I can see everything with Robo 3T.
Now I want to protect my database.
I have tried to set authentication for the database. I followed https://docs.mongodb.com/manual/tutorial/enable-authentication/ . It did not work.
I still can reach mongodb with robo 3t and see all information.
I have also tried to start mongodb with --auth parameter. I have define a configuration file looks like
And for starting mongodb
mongod -f C:\mongodb\conf\mongodb.conf
Mongodb has been started but, it did not ask any pass. And I can save simple data with Postman without authentication.
What I want to do:
Protect my database against Robo 3t. :))
I dont want to save any data without auth.
Building Nodejs connection string that include pass like
mysql://root:password#localhost:port/dbName
Here is my Nodejs index.js code
this is my model.
PS: I am very new in Nodejs and Mongodb.
EDIT: inside conf file

MongoDB Cloud9 Connection

So, I am wondering if there is a way to connect to the mongoDB I have setup in my Cloud9 from an html. I mean, I have already connected to the db from the terminal and everything is working like a charm but I need to do some stuff inside my script in an html document and when I try calling the function which contains this code it does nothing
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/ingesoft', function (err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
db.close();
});
I have saved the same code into a "file.js" and ran it from console using node file.js and it outputs into the console log "successfully connected to the database", plus the terminal which is running mongo's connection shows me one more connection to the db. The thing is, when I try to run that code inside my script it doesn't work. Sorry for my ignorance I am new to mongo.
Any help would be much appreciated
To simplify your question, here's what's going on:
node file.js containing the code in your question is working
pasting the same code to your html file is not
So, getting to the bottom of the issue, let's ask first: what's the difference between running node file.js and putting the code in html?
The difference is that node ... is running on your Cloud9 workspace (let's call it the server machine).
Your MongoDB server is also running on that server machine
The mongodb npm package you installed is also present on the server machine
The url: mongodb://127.0.0.1:27017/ingesoft references 127.0.0.1 which is the localhost for your server
whereas with the code on your browser:
The code is being run on your customer's machine
That machine doesn't have your Mongodb server
Browser's usually don't support require
You can do requires if you bundle code and use something like webpack or browserify. Did you perhaps do that?
If you did indeed package everything, was the mongodb package that you're requiring packaged?
Can the mongodb package be run from the client side?
The url: mongodb://127.0.0.1:27017/ingesoft references 127.0.0.1 which is the localhost for your customer's machine
Basically, as you can see from the above, the two are very different.
If you want to talk to your db, a lot of people go the following route:
Make a server application that implements some form of REST API
That REST API talks to your DB
Your client code knows how to talk to the REST API and get the required data
That way, you only talk to your MongoDB using your server, and the client can talk to your server via the internet.
This is, of course, an oversimplification, but I hope this resolves your confusion.

I need to create a simple database for use in an Angular/Node demo. What is the path of least resistance to adding such a database to my demo?

I need to create a simple database for use in an Angular/Node demo. What is the path of least resistance to adding such a database to my demo?
Incidentally, I cannot use the MEAN stack.
Edit: I failed to mention that I'm running the demo from my local environment--from my laptop.
The path of least resistance would be to use Sequelize and sqlite3 in your application.
Start off by running npm install sequelize sqlite3.
Then in your code
var Sequelize = require( 'sequelize' )
var connection = Sequelize.connect( undefined, {
dialect: 'sqlite',
storage: __dirname + '/db.sqlite'
})
Tada, you have a database connection syncing to a local sqlite DB.
You'll likely need to consult the Sequelize documentation to use this connection.
If, however, you wanted an API for your angular demo, json-server is what you're looking for.

Where Does Meteor Store It's Data?

I have just started playing around with Meteor and MongoDB for the first time. I come from a .net developer background and MSSQL. I have created a simple web following this tutorial.
Also, I have added the accounts-password package to my app as well. Everything works like a charm, I can add data to my application in real time and I can create users and login, etc, etc. However, when I open up Robomongo and look for the stored data, I do not find any information inside my 'carbrands' collection declared in carbrands.js as
CarBrands= new Meteor.Collection("carbrands");
Also, no users collection is created. My Robomongo profile is connected to localhost. If I try to access CarBrands from Chrome console i get the following output:
> CarBrands.find();
> LocalCollection.Cursor {collection: LocalCollection, selector_id: undefined, selector_f: function, sort_f: null, skip: undefined…}
Where is the meteor data being saved to and how can i view it ?
UPDATE**: I have run mongo command in linux terminal and the result returned are local and test. test is empty. Accessing local returns the same collections as viewed in Robomongo
You need to run:
meteor mongo
from your project, not your usual mongoDB install:
http://docs.meteor.com/#meteormongo
Meteor stores local mongo datasets in APPDIR/.meteor/local/db.
A ps will verify DB location.
ps -ax | grep mongo
1482 ? Sl 170:02 /usr/bin/mongod --config /etc/mongodb.conf
2176 ? Sl 174:46 /home/ell/.meteor/packages/meteor-tool/.1.4.2_3.qrhx4m++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/mongodb/bin/mongod --bind_ip 127.0.0.1 --port 3001 --dbpath /apps/assessment/current/.meteor/local/db --oplogSize 8 --replSet meteor --nojournal

Create MongoDB server in Node.js and use it with Mongoose

Is there any way to create and initiate a MongoDB server and a DB inside Node.JS and use Mongoose with that MongoDB server? I want to rent a VPS and use it with Node.JS, but I don't know how to run a Node.JS and MongoDB server at the same time in a VPS. I only have used MongoDB and Node.JS in my mac.
PD: Sorry for my English.
Maybe you're confusing between mongoDB database and Node.js application.
You don't run a mongoDB IN Node.js, they are two different things and processes.
First of all you must have a mongoDB instance running on the machine.
Here's only one example to run it as a service on a Linux server.
Node.js is a server side environment that among other things can CONNECT to a mongoDB instance to store o retrieve data. You have to install it as well on your server.
Mongoose is a Node.js module that "mediates" the connection from a Node.js application to MongoDB and give you a set of utility to interact with your mongo database.

Categories

Resources