sudo pacman -S mongodb sudo systemctl enable mongodb.service sudo systemctl start mongodb.service
mongo
mongo | sql |
---|---|
collection | table |
document | record |
Insert a record (document) into a table called userlist
db.userlist.insert({'username' : 'test1', 'email' : 'test1@test.com', 'gender' : 'Male'})
To empty the userlist
collection do:
db.userlist.remove()
db.dropDatabase()
show dbs
show collections
from command line, NOT MONGO SHELL!!!
json file | data.js |
collection | people |
database | simple |
$ mongoimport --db simple --collection people --jsonArray data.js
collection: posts
db.posts.find()
npm install -g express-generator
Now lets say our project is going to be called: abc123
, scaffold it
out with:
express --ejs abc123 cd abc123; npm install
Tell node/express that we want to use mongoose
as our javascript
library to interact with MongoDB.
npm install --save mongoose
To connect to a database called test
, in app.js
put:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
TEST you can now test the connection by seeing if starting node throws any errors
npm start
Now lets create a model called Posts
which holds blog posts. Lets
create a folder to hold our models
mkdir Models
Here create the file Posts.js
with the following contents:
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
body: String,
upvotes: {type: Number, default: 0},
});
mongoose.model('Post', PostSchema);
Now we want to interact with this model via REST. We will setup routes for the GET action.
routes/index.js
var mongoose = require('mongoose');
var Post = mongoose.model('Post');
router.get('/posts', function(req, res, next) {
Post.find(function(err, posts){
if(err){ return next(err); }
res.json(posts);
});
});
In app.js
we need to declare the Posts schema/model before it is
used in the routes:
require('./models/Posts');
var routes = require('./routes/index');
TEST: Now we can test!
Start Node
npm start
Insert a post via mongodb shell
$ mongo
> db.posts.insert({'title' : 'test title', 'body' : 'This is the body.', 'upvotes': 0})
Query all posts
with CURL
curl http://localhost:3000/posts
router/index.js
router.post('/posts', function(req, res, next) {
var post = new Post(req.body);
post.save(function(err, post){
if(err){ return next(err); }
res.json(post);
});
});
TEST: Insert doc with CURL
curl --data 'short_description=Kensignton32GBUSB3.0Thumbdrive&price=500' http://localhost:3000/posts