简体   繁体   中英

Issue with updating a document in MongoDB

I'm finding problem in updating the documents which are inserted into the DB. I have created a web page where I can insert the document into the DB and retrieve and display it on the same page. However, I'm not able to update the existing document.

When I try update the document using the objectID , the data is not getting updated.

I would like help or suggestions on what to do.

FYI: the DB I have used here is 'iot' and the collection is : 'metadata'

I'm a beginner in JS and MongoDB. I would kindly need some help with this.

 var express = require('express'); var router = express.Router(); var assert = require('assert'); var url = "mongodb://localhost:27017"; const MongoClient = require('mongodb').MongoClient; const client = new MongoClient(url); const dbName = 'iot'; /* GET home page. */ router.get('/', function(req, res, next) { res.render('index'); }); router.get('/get-data', function(req, res, next){ var resultArray = []; MongoClient.connect(url, function(err, client){ assert.equal(null, err); const db = client.db(dbName); var cursor = db.collection('metadata').find(); cursor.forEach(function(doc, err) { assert.equal(null, err); resultArray.push(doc); }, function(){ client.close(); res.render('index', {items: resultArray}); }); }); }); router.post('/insert', function(req, res, next) { var item = { title: req.body.title, content: req.body.content, author: req.body.author } MongoClient.connect(url, function(err, client){ assert.equal(null, err); const db = client.db(dbName); db.collection('metadata').insertOne(item, function(err, result){ assert.equal(null, err); console.log('Item inserted'); client.close(); }); }); res.redirect('/'); }); router.post('/update', function(req, res, next) { var item = { title: req.body.title, content: req.body.content, author: req.body.author }; var id = req.body.id; MongoClient.connect(url, function(err, client){ assert.equal(null, err); const db = client.db(dbName); db.collection('metadata').updateOne({"_id": objectId(id)}, {$set: item}, function(err, result) { assert.equal(null, err); console.log('Item updated'); db.close(); }); }); ///res.redirect('/'); }); router.post('/delete', function(req, res, next) { var id = req.body.id; mongo.connect(url, function(err, db) { assert.equal(null, err); db.collection('metadata').deleteOne({"_id": objectId(id)}, function(err, result) { assert.equal(null, err); console.log('Item deleted'); db.close(); }); }); }); module.exports = router;
 <h1>HOME DASHBOARD </h1> <section class="insert"> <h3>Insert Data</h3> <form action="/insert" method="post"> <div class="input"> <label for="title">Title</label> <input type="text" id="title" name="title"> </div> <div class="input"> <label for="content">Content</label> <input type="text" id="content" name="content"> </div> <div class="input"> <label for="author">Author</label> <input type="text" id="author" name="author"> </div> <button type="submit">INSERT</button> </form> </section> <section class="get"> <h3>Get Data</h3> <a href="/get-data">LOAD DATA</a> <div class="gfg"> <div> {{# each items }} <article class="item"> <div>Title: {{ this.title }}</div> <div>Content: {{ this.content }}</div> <div>Author: {{ this.author }}</div> <div>ID: {{ this._id }}</div> </article> {{/each}} </div> </div> </section> <section class="update"> <h3>Update Data</h3> <form action="/update" method="post"> <div class="input"> <label for="id">ID</label> <input type="text" id="id" name="id"> </div> <div class="input"> <label for="title">Title</label> <input type="text" id="title" name="title"> </div> <div class="input"> <label for="content">Content</label> <input type="text" id="content" name="content"> </div> <div class="input"> <label for="author">Author</label> <input type="text" id="author" name="author"> </div> <button type="submit">UPDATE</button> </form> </section> </section> <section class="delete"> <h3>Delete Data</h3> <form action="/delete" method="post"> <div class="input"> <label for="id">ID</label> <input type="text" id="id" name="id"> </div> <button type="submit">DELETE</button> </form> </section>

try importing the ObjectID , i don't see it there

const url = 'mongodb://localhost:27017';
const client = new MongoClient(url, {useUnifiedTopology: true});


var ObjectID = require('mongodb').ObjectID;

// In your request
{ "_id": ObjectID(req.body.id)}, // Filter

//in your code 
client.connect(function(err, client){
    assert.equal(null, err);
    const db = client.db(dbName);
    db.collection('metadata').updateOne({ "_id": ObjectID(req.body.id)}, {$set: item}, function(err, result) {
      assert.equal(null, err);
      console.log('Item updated');
     db.close();
    });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM