-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (27 loc) · 1008 Bytes
/
index.js
File metadata and controls
32 lines (27 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var mongo = require('mongodb')
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/newdb";
MongoClient.connect(url,
function(err, db) {
if (err) throw err;
console.log("Database connected!");
var dbo = db.db("newdb");
// Insertion (one et many avec un array json)
dbo.collection("students").insertOne({"name":"Abhishek","marks":100}, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
dbo.collection("students").insertMany([{"name":"John","marks":90},{"name":"Tim","marks":80}], function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
// Remove a record :
// dbo.collection("students").remove({marks:90});
// Filter results : (pour tout afficher, mettre find({}))
var results = dbo.collection("students").find({marks:90});
results.forEach(row => {
console.log(row);
});
});