You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
2.3 KiB
26 lines
2.3 KiB
Code listing upvotes.txt |
|
article.append(addInsetCodeListing(["await db.collection('articles').updateOne({ name }, {", " $inc: { upvotes: 1 },", "})"])) |
|
article.append(addParagraph("This might look a little strange but the updateOne method is similar to the findOne method in that it will only update the article with the appropriate name.")) |
|
article.append(addParagraph("It might be a little difficult to see with the code spaced out as it is but the updateOne function takes two arguments, the first being the article name, in other words, the thing in the database that you want to perform an operation on and the change that you want to make. The change is")) |
|
article.append(addInset("$inc: { upvotes: 1 },")) |
|
article.append(addParagraph("The first part of that is the operation. It is increment in this case and $inc is just how mongodb expects you to specify an increment operation. The last part of that is the \"field\" you are updating followed by the amount you want to increment it by (strictly speaking, this is the number of times you want to increment it but that's an academic point - either way if you put, say, 5 there instead of the 1, the upvotes counter would increase by 5).")) |
|
|
|
|
|
Code listing comments.txt |
|
article.append(addInsetCodeListing(["await db.collection('articles').updateOne({ name }, {", " $push: { comments: { postedBy, text } },", "})"])) |
|
|
|
|
|
Code listing db.js |
|
article.append(addInsetCodeListing(["import { MongoClient } from 'mongodb';", "", "let db;", "", "async function connectToDb(cb) {", " const client = new MongoClient('mongodb://127.0.0.1:27017');", " await client.connect();", " db = client.db('react-blog-db');", " cb();", "}", "", "export {", " db," " connectToDb,", "}"])) |
|
|
|
|
|
Code listing deleted code |
|
article.append(addInsetCodeListing(["const client = new MongoClient('mongodb://127.0.0.1:27017');", "await client.connect();", "", "const db = client.db('react-blog-db');"])) |
|
|
|
|
|
Code listing call connect |
|
article.append(addInsetCodeListing(["connectToDb(() => {", " console.log('Successfully connected to the database!')", " app.listen(8000, () => {", " console.log('Server is listening on port 8000');", " });", "});"])) |
|
|
|
|
|
Inset findOne |
|
article.append(addInset("const article = await db.collection('articles').findOne({ name });"))
|
|
|