-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.js
64 lines (53 loc) · 1.55 KB
/
mongodb.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
// CRUD create read update delete
const { MongoClient, ObjectID } = require('mongodb')
const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'
const id = new ObjectID()
console.log(id.id.length)
console.log(id.toHexString().length)
MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error, client) => {
if (error) {
return console.log('Unable to connect to database!')
}
const db = client.db(databaseName)
db.collection('users').insertOne({
name: 'foo',
age: 26
}, (error, result) => {
if (error) {
return console.log('Unable to insert user')
}
console.log(result.ops)
})
// db.collection('users').insertMany([
// {
// name: 'Jen',
// age: 28
// }, {
// name: 'Gunther',
// age: 27
// }
// ], (error, result) => {
// if (error) {
// return console.log('Unable to insert documents!')
// }
// console.log(result.ops)
// })
// db.collection('tasks').insertMany([
// {
// description: 'Clean the house',
// completed: true
// },{
// description: 'Renew inspection',
// completed: false
// },{
// description: 'Pot plants',
// completed: false
// }
// ], (error, result) => {
// if (error) {
// return console.log('Unable to insert tasks!')
// }
// console.log(result.ops)
// })
})