This shows you the differences between two versions of the page.
se:labs:08 [2023/12/05 19:26] avner.solomon |
se:labs:08 [2023/12/06 20:56] (current) avner.solomon |
||
---|---|---|---|
Line 59: | Line 59: | ||
// Connect to MongoDB database using a MongoDB client for Node | // Connect to MongoDB database using a MongoDB client for Node | ||
let db | let db | ||
- | const client = new mongodb.MongoClient.connect('mongodb://localhost:27017/') | + | const client = new mongodb.MongoClient('mongodb://localhost:27017/') |
+ | client.connect() | ||
const connectToDB = async () => { | const connectToDB = async () => { | ||
await client.connect() | await client.connect() | ||
Line 78: | Line 79: | ||
app.get('/api/tasks', async (req, res) => { | app.get('/api/tasks', async (req, res) => { | ||
//Find all documents in the tasks collection and returns them as an array. | //Find all documents in the tasks collection and returns them as an array. | ||
- | return { | + | return res.json({ |
tasks: await db.collection('tasks').find().toArray() | tasks: await db.collection('tasks').find().toArray() | ||
- | } | + | }) |
}) | }) | ||
Line 182: | Line 183: | ||
let db //our db instance | let db //our db instance | ||
- | mongodb.MongoClient.connect('mongodb://localhost:27017/', (err, client) => { | + | const client = new mongodb.MongoClient('mongodb://localhost:27017/') |
- | // connecting to our local database using the MongoDB client for Node | + | client.connect() |
- | if (err) return console.log(err) | + | const connectToDB = async () => { |
+ | await client.connect() | ||
console.log('Connected to database') | console.log('Connected to database') | ||
- | // get our db instance | + | db = client.db('university-db') |
- | db = client.db('university-db'); | + | } |
- | }) | + | connectToDB() |
</code> | </code> | ||
Line 202: | Line 204: | ||
let db | let db | ||
- | mongodb.MongoClient.connect('mongodb+srv://<user>:<parola>@cluster......mongodb.net/?retryWrites=true&w=majority', (err, client) => { | + | const client = new mongodb.MongoClient.connect(''mongodb+srv://<user>:<parola>@cluster......mongodb.net/?retryWrites=true&w=majority') |
- | if (err) return console.log(err) | + | const connectToDB = async () => { |
+ | await client.connect() | ||
console.log('Connected to database') | console.log('Connected to database') | ||
- | db = client.db('university-db'); | + | db = client.db('university-db') |
- | }) | + | } |
+ | connectToDB() | ||
</code> | </code> | ||
Line 212: | Line 216: | ||
<code javascript> | <code javascript> | ||
- | server.get('/api/tasks', (req, res) => { | + | server.get('/api/tasks', async (req, res) => { |
- | db.collection('tasks').find().toArray((err, result) => { | + | return res.send({ |
- | if (err) return console.log(err) | + | tasks: await db.collection('tasks').find().toArray() |
- | res.send({ | + | |
- | tasks: result | + | |
- | }) | + | |
}) | }) | ||
}) | }) | ||
Line 231: | Line 232: | ||
<code javascript> | <code javascript> | ||
- | db.collection('tasks').find().toArray((err, result) => { | + | const documents = await db.collection('tasks').find().toArray() |
- | if (err) return console.log(err) | + | |
- | //do something with result | + | |
- | console.log(result) | + | |
- | }) | + | |
</code> | </code> | ||
If we want to get a single document we can do it using its **id** (or any other field), the next code snippet will find the document with id 123: | If we want to get a single document we can do it using its **id** (or any other field), the next code snippet will find the document with id 123: | ||
<code javascript> | <code javascript> | ||
- | db.collection('tasks').findOne({"_id": 123}).then((err, result) => { | + | const doc = await db.collection('tasks').findOne({"_id": 123}) |
- | if (err) return console.log(err) | + | |
- | //do something with result | + | |
- | console.log(result) | + | |
- | }) | + | |
</code> | </code> | ||
Line 250: | Line 243: | ||
<code javascript> | <code javascript> | ||
- | db.collection('tasks').find({ qty: { $gt: 4 } }).toArray((err, result) => { | + | const docs = aiwat db.collection('tasks').find({ qty: { $gt: 4 } }).toArray() |
- | if (err) return console.log(err) | + | |
- | //do something with result | + | |
- | console.log(result) | + | |
- | }) | + | |
</code> | </code> | ||
Line 260: | Line 249: | ||
<code javascript> | <code javascript> | ||
- | db.collection('tasks').updateOne( | + | await db.collection('tasks').updateOne( |
{ _id: 1 }, | { _id: 1 }, | ||
{ $set: { | { $set: { | ||
Line 273: | Line 262: | ||
<code javascript> | <code javascript> | ||
- | db.collection('tasks').deleteOne( | + | await db.collection('tasks').deleteOne( |
{ title: "ABC123" } | { title: "ABC123" } | ||
) | ) |