Skip to content

Commit 3eaa551

Browse files
committed
added update document functionality
1 parent 3e9b49b commit 3eaa551

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

mongodb/node/api_docs_server.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,53 @@ app.post("/document", async (req, res, next) => {
116116
}
117117
});
118118

119+
/**
120+
* @api {put} /document/:id Update a document in the collection.
121+
* @apiVersion 0.1.0
122+
*
123+
* @apiName Update_Document
124+
* @apiGroup MongoDB Document Endpoints
125+
*
126+
* @apiParam {Object} hero Updated hero object
127+
*
128+
* @apiSuccess {String} result Success or failure.
129+
*
130+
* @apiSuccessExample Success-Response:
131+
* HTTP/1.1 200 OK
132+
* {
133+
* "result": "success"
134+
* }
135+
*
136+
* @apiError Unable_To_Update Failed to update document in collection.
137+
*
138+
* @apiErrorExample Success-Response:
139+
* HTTP/1.1 503 OK
140+
* {
141+
* "result": "failure"
142+
* }
143+
*/
144+
app.put("/document/:id", async (req, res, next) => {
145+
146+
let id = req.params.id;
147+
let updatedHero = req.body;
148+
149+
try {
150+
let update = await mongoDBService.HeroesDb.Update({"name": id}, updatedHero);
151+
152+
if (modifiedCount == 1) {
153+
res.json({"result": "success"});
154+
} else {
155+
res.status(503).json({"result": "failure"});
156+
}
157+
158+
} catch(e) {
159+
e.httpStatusCode = 500;
160+
e.customMsg = `Error inserting document: ${hero}`;
161+
162+
return next(e)
163+
}
164+
});
165+
119166
/**
120167
* @api {post} /documents Insert multiple documents into the collection.
121168
* @apiVersion 0.1.0

0 commit comments

Comments
 (0)