I saw job offets vor javascriptdevelopers in London Well paid
It was mentioned thath the knowledge of MongoDBNode is advisable almost required:
As My tactics to remember better and spread my activity through internet which gives enromous power I retyped Mongo Db researching the book more I notice there is also possibilty to remove the data and others:
As long as I undertsand the database allows to sort, search the data, text at some points:
ownload the mongo
Or in Windows, run the .exe file from the command prompt:
c:mongodbbinmongod.exe
– The MongoDb Native Node.js Driver
Mongoose
npm install mongodb
Creatinmg database:
var mongodb = require (‘mongodb’);
Then create a new database :
var dbServer = new mongodb.Server(‘localhost’, 27017, {auto_reconnect:
true]),
var db = new mongodb.Db (;mydb;, dbServer, {w: 1 });
creating collections:
db.open(function(err, conn) {
});
Adding a collection
adding collection to the database
db.open( function(err, conn) {
// add a collection to the db
db.collection(‘myCollection’, function(err, collection) {
// insert a document into the collection
collection. insert( {
a: ‘my item’
}, function (err, result) {
// log the result
console.log(result);
// close the connection
db.close();
});
});
});
After that run the script in node
Poicture shiws the database and collection created
Reading data
After adding documents to the database you can read them using find()
But before using the find() API create a new collection to work with :
// open database connection
db.open ( function(err, conn) {
// selet the collection
db. collection(‘MyNewCollection’, function(err, collection) {
// cache a count variable
var count = 0;
// insert numbers into the collection
for (var i = 0; i <5; i++ ) {
collection. insert ({
num: i
}, function (err, result) {
// log the result
console.log(result);
// increment the count value
count ++;
// if the count is high enough, close the connection
if ( count > 4 ) {
db.close();
}
});
}
});
});
Select all Entries in a collection
Now that a collection has been created you can select all the data in the database :
db.open ( function (err, conn) {
/ select the collection
db. collection(‘myNewCollection’, function(err, collection) {
// select all the documents in the collection
collection.find().toArray(function(err, result) {
// log the data
console.log(result);
//close the connection
db.close();
});
});
});
Select Specific Entries:
db.collection(‘myNewCollection’,function(err, collection) {
// select all documents where num is 2
collection.find ({num: 2 }).toArray(function(err, result) {
//log the data
console.log(result);
//close the connection
db.close();
});
});
More advances Query selectors:
//select the collection
db.collection(‘MyNewCollection’, function(err, collection) {
// select numbers greater than 1
collection.find({num: {$gt:1}}).toArray(function(err, result) {
//log the data
console.log(result);
// close the connection
db.close();
});
});
You can combine additional parameters in the query object. For instance, you can matcg all tge values that are greater than 1 but also less than 1
db.collection(‘myNewCollection’, function(err, collection) {
// select numbers greater than 1 but less than 4
collection. find({num: {$gt: 1, $lt: 4 }}).toArray(function(err, result) {
// log the data
console.log(result);
// close the connection
db.close();
});
})
;
You can also limit the number of documents the search returns:
db.collection(‘myNewCollection’, function(err, collection) {
// select all the data in the collection, limited to 3 entries
collection.find({}, {limit: 3 }) .toArray(function(err, result) {
// log the data
console.log(result);
// close the connection
db.close();
});
});
Additionally, you can sort the documents in the collection when selectin them
db.collection(‘myNewCollection’, function(err, colection) {
//sort the collection in reverese order
collection.find{}, {sort:[{‘num’, ‘desc’]]}). toArray(function(err, result) {
// log the data
console.log(result);
// close the connection
db.close();
});
});