Installation
[js]C:\mongodb\bin\mongod.exe –dbpath “C:\Users\abc\Desktop\[database folder path]”[/js]
Practical usage
INSERT
[js]
let data = [
{
“url”: “abc”,
“price_now”: “$330”
},
{
“url”: “def”,
“price_now”: “$220”
},
{
“url”: “ghi”,
“price_now”: “$110”
}
];
var mongoClient = require(‘mongodb’).MongoClient;
var url = ‘mongodb://localhost:27017/dbfolder’
mongoClient.connect(
url, function(err, db) {
if (err) throw err
else
console.log(‘Connected!!!’);
var dbo = db.db(“db_name”);
for (let i = 0; i < data.length; i++) {
// Insert
dbo.collection("collection_name").insertOne(data[i], function(err, res) {
if (err) throw err;
console.log("1 product inserted");
});
}
db.close();
}
);
[/js]
SELECT
[sql]
— MySQL
select * from products where url = ‘abc’ and price_now = ‘$330’
[/sql]
[js]
— NoSQL
var mongoClient = require(‘mongodb’).MongoClient;
var url = ‘mongodb://localhost:27017/dbfolder’
mongoClient.connect(
url, function(err, db) {
if (err) throw err
else
console.log(‘Connected!!!’);
var dbo = db.db(“db_name”);
// Filter out
var query = { url: “abc”, price_now: “$330” };
dbo.collection(“products”).find(query).toArray(function(err, result) {
if (err) throw err;
console.log(result);
});
db.close();
}
);
[/js]
Leave a Reply