Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (82 loc) · 2 KB
/
Copy pathapp.js
File metadata and controls
99 lines (82 loc) · 2 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// const MongoClient = require('mongodb').MongoClient;
const {MongoClient, ObjectID} = require('mongodb');
const config = require('./config/config');
var obj = new ObjectID();
console.log(obj);
MongoClient.connect(config.database, (err, client) => {
if(err){
return console.log('Unable to connect to Mongo Database!');
}
console.log('Connected to Mongo Server');
const db = client.db(config.databaseName);
/*
* Insert Data
*/
// db.collection('Users').insertOne({
// name: 'Sovath',
// age: 21,
// location: 'Phnom Penh'
// }).then((result) => {
// console.log(result);
// });
/*
* Fetch All Data with Array
*/
// db.collection('Users').find().toArray().then((docs) => {
// console.log('Users');
// console.log(JSON.stringify(docs, undefined, 2));
// }, (err) => {
// console.log('Unable to fecth users', err);
// });
/*
* Fetch All Data with Array With Condition
*/
// db.collection('Users').find({ name: 'David') }).toArray().then((docs) => {
// console.log('Users');
// console.log(JSON.stringify(docs, undefined, 2));
// }, (err) => {
// console.log('Unable to fecth users', err);
// });
/*
* Get Count all data
*/
// db.collection('Users').find().count().then((count) => {
// console.log(`Total User: ${count}`);
// }, (err) => {
// console.log('Unable to fecth users', err);
// });
/*
* DeleteMany
*/
// db.collection('Users').deleteMany({ name: 'David' }).then((result) => {
// console.log(result);
// });
/*
* DeleteOne
*/
// db.collection('Users').deleteOne({ name: 'David' }).then((result) => {
// console.log(result);
// });
/*
* FindOneAndDelete
*/
// db.collection('Users').FindOneAndDelete({ location: 'Phnom Penh' }).then((result) => {
// console.log(result);
// });
/*
* Update User
*/
// db.collection('Users').findOneAndUpdate({
// name: 'Johny'
// },{
// $set: {
// location: 'United State'
// },
// $inc: {
// age: -3
// }
// }).then((result) => {
// console.log(result);
// });
// client.close();
});