-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_db.js
72 lines (61 loc) · 2.53 KB
/
mongo_db.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017/";
MongoClient.connect(url, function (err, db) {
if (err) throw err;
let con = db.db("mydb");
con.collection("customers").find().limit(4).toArray(function (err, res) {
if (err) throw err;
res.forEach(function (re) {
console.log(re._id, re.name, re.address);
});
db.close();
});
});
// let query = {address: /^S/};
// let val = { $set: {name: "locus"}};
// con.collection("customers").updateMany(query, val, function (err, res) {
// if (err) throw err;
// console.log(res.result.nModified + ' record(s) updated!');
// let query = {address: "Valley 345"};
// let val = { $set: {name: "Mickey", address: "Canyon 123" }};
// con.collection("customers").updateOne(query, val, function (err, res) {
// if (err) throw err;
// console.log('1 record updated!');
// let query = {address: /^O/};
// con.collection("customers").deleteMany(query, function (err, res) {
// if (err) throw err;
// console.log(res.deletedCount + ' record deleted!');
// db.close();
// });
//### drop collictions ###
// MongoClient.connect(url, function(err, db) {
// if (err) throw err;
// var dbo = db.db("mydb");
// dbo.collection("customers").drop(function(err, delOK) {
// if (err) throw err;
// if (delOK) console.log("Collection deleted");
// db.close();
// });
// });
// let mySort = {name: -1}; { name: 1 } for ascending and{ name: -1 } for descending
// con.collection("customers").find().sort(mySort).toArray( function (err, res) {
// res.forEach(function (re) {
// console.log("-".repeat(50));
// console.log(re._id, re.name, re.address);
// });
// let query = {address: /^S/};
// con.collection("customers").find(query).toArray( function (err, res) {
// con.collection("customers").find(query, {_id: 0, name: 1}).toArray( function (err, res) {
// con.collection("customers").find({},{_id: 0,address:1}).toArray( function (err, res) {
// if (err) throw err; ####always except _id : 0 if you want to hide it.####
// console.log(res);
// let myobj = {name: "devian inc", address: "Highway 37"};
// con.collection("customers").insertOne(myobj, function (err, res) {
// if (err) throw err;
// console.log('1 document inserted!');
// db.close();
// });
// let myobj = [{k1:'v1'},{k2:'v2'},...]
// con.collection("customers").insertMany(myobj, function (err, res) {
// if (err) throw err;
// console.log(res.insertedCount + " document(s) inserted !");