Driver
Mongos is a ODM way to interact with MongoDB
Assert package
Package to assert conditions
Install
npm install --assert --save
Install MongoDB in your project
npm install --mongodb --save
Execute MonogDB
- In a console change the path to the folder mongodb
- Type: mongod --dbpath=data
- In another console type: mongo
- Type: use <DB>
- In case you want to raise your project go to the folder in another tap and type: node <file>.js
Insert a register in a project
var MongoClient = require('mongodb').MongoClient, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/<DB>';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
assert.equal(err,null);
console.log("Connected correctly to server");
var collection = db.collection("dishes");
collection.insertOne({name: "Uthapizza", description: "test"}, function(err,result){
assert.equal(err,null);
console.log("After Insert:");
console.log(result.ops);
collection.find({}).toArray(function(err,docs){
assert.equal(err,null);
console.log("Found:");
console.log(docs);
db.dropCollection("dishes", function(err, result){
assert.equal(err,null);
db.close();
});
});
});
});
Comentarios
Publicar un comentario