on
Italy
- Get link
- X
- Other Apps
// update your packages
$ brew update
// install mongoDB
$ brew install mongodb
$ mongod
waiting for connections on port 27017mongod starts MongoDB, the command to connect to it is:$ mongo
$ show dbs
use a database (even if it’s not created), create a collection and document, and everything will automatically be made for you!$ db
$ use db_name
// save one user
$ db.users.save({ name: 'Chris' });
// save multiple users
$ db.users.save([{ name: 'Chris'}, { name: 'Holly' }]);
By saving a document into the users collection of the database you are currently in, you have successfully created both the database and collection if they did not already exist.// show all users
$ db.users.find();
// find a specific user
$ db.users.find({ name: 'Holly' });
db.users.update({ name: 'Holly' }, { name: 'Holly Lloyd' });
// remove all
db.users.remove({});
// remove one
db.users.remove({ name: 'Holly' });
This is just a quick overview of the types of commands you can run. The MongoDB docs are quite comprehensive and provide a great deal of detail for those that want to dive deeper.$ mongod
We will also give mongoose a database name to connect to (it doesn’t need to be created since mongoose will create it for us).// grab the packages we need
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db_name');
That’s it! Once we start saving things into our database, that database named db_name will automatically be created.localhost and the port to 27017. Name your connection anything you want.
Comments
Post a Comment