-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb2.js
50 lines (45 loc) · 1.34 KB
/
mongodb2.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
//Read
const { MongoClient, ObjectID } = require('mongodb');
const connectionURL = 'mongodb://127.0.0.1:27017';
const databaseName = 'task-manager';
MongoClient.connect(
connectionURL,
{ useNewUrlParser: true, useUnifiedTopology: true },
(error, client) => {
if (error) {
return console.log('Unable to connect to database');
}
const db = client.db(databaseName); //Give reference to the specific database you want to manipulate
db.collection('users').findOne(
{ _id: new ObjectID('5ec9baa3b36d512b3878dc94') },
(error, user) => {
if (error) {
return console.log('Unable to fetch');
}
console.log(user);
}
); //The object is used to specify our search criteria
//To fetch multiples pieces of data
// db.collection('users')
// .find({ age: 20 })
// .toArray((error, users) => {
// console.log(users);
// });
// db.collection('users')
// .find({ age: 20 })
// .count((error, count) => {
// console.log(count);
// });
db.collection('new tasks').findOne(
{ _id: new ObjectID('5ec9a52867ab9f1fc803cf67') },
(error, task) => {
console.log(task);
}
);
db.collection('new tasks')
.find({ completed: false })
.toArray((error, task) => {
console.log(task);
});
}
);