-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.js
87 lines (78 loc) · 2.58 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
///Users/Acer/mongodb/bin/mongod.exe --dbpath=/Users/Acer/mongodb-data
//Create
// const mongodb = require('mongodb');
//to initialize the connection function necessary to connect to the database to perform CRUD ops
// const MongoClient = mongodb.MongoClient;
// const ObjectID = mongodb.ObjectID;
const { MongoClient, ObjectID } = require('mongodb');
//define the connection url and the database we are trying to connect to
const connectionURL = 'mongodb://127.0.0.1:27017'; //localhost was not used as it causes unstable behaviour
const databaseName = 'task-manager'; //can be any name
// const id = new ObjectID();
// console.log(id);
// console.log(id.getTimestamp());
MongoClient.connect(
connectionURL,
{ useNewUrlParser: true, useUnifiedTopology: true },
(error, client) => {
if (error) {
return console.log('Unable to connect to database');
}
//To insert document
//Give reference to the specific database you want to manipulate
const db = client.db(databaseName);
// //To insert a single document in the 'users' collection
// db.collection('users').insertOne(
// {
// _id : id, //Here a customized id can be provided
// name: 'WackadOODLE',
// age: 200,
// },
// (error, result) => {
// if (error) {
// return console.log('Unable to insert user');
// }
// console.log(result.ops);
// }
// ); //It expects the name of the collection you are trying to manipulate
// //InsertOne expects an object as that first argument, and this should contain all of the data you are trying to insert
//To insert multiple documents
// db.collection('users').insertMany(
// [
// {
// name: 'Jen',
// age: 19,
// },
// {
// name: 'Devon',
// age: 50,
// },
// ],
// (error, result) => {
// if (error) {
// return console.log('Unable to insert documents');
// }
// console.log(result.ops);
// }
// );
// db.collection('new tasks').insertMany(
// [
// {
// description: 'pehla',
// completed: true,
// },
// {
// description: 'the second one',
// completed: false,
// },
// ],
// (error, result) => {
// if (error) {
// return console.log('Error hogya re');
// }
// console.log(result.ops);
// }
// );
}
);
// useNewURLParser for our url's to get parsed correctly, the cbk function will be called when we're connected to the database