Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.updateOne on MongoDB not working in Node.js

I have the following code:

connection((db) => {             db.collection('orders')                 .updateOne(                     { "_id": req.body._id}, // Filter                     {"name": req.body.name} // Update                 )                 .then((obj) => {                     console.log('Updated - ' + obj);                     res.redirect('orders')                 })                 .catch((err) => {                     console.log('Error: ' + err);                 })         }) 

I want to change the name in the order but it doesn't update it. The result in the console is

Updated - {"n":0,"nModified":0,"ok":1}

I tried to read the documentation but it's horrific

EDIT: {$set: {"name": req.body.name}}, didn't work as well

EDIT 2: The passed ID matches the _id in the database. Could it be a problem that I'm querying for plain text ID while in the database it is referred to as "ObjectId('5a42ja...')"

like image 787
Simeon Nakov Avatar asked Dec 05 '17 14:12

Simeon Nakov


People also ask

How do I use updateOne?

The updateOne() method adds the specified field if it does not exist in a matching document. For example, the following will add the location field. Execute the following find() method to see the updated data. Use the $inc update operator to increase the value of the field by the specified amount.

How do you update a specific field in MongoDB using node JS?

You can update a record, or document as it is called in MongoDB, by using the updateOne() method. The first parameter of the updateOne() method is a query object defining which document to update. Note: If the query finds more than one record, only the first occurrence is updated.

How do I update a node node in MongoDB?

Node.js MongoDB Update. ❮ Previous Next ❯. You can update a record, or document as it is called in MongoDB, by using the updateOne() method. The first parameter of the updateOne() method is a query object defining which document to update. Note: If the query finds more than one record, only the first occurrence is updated.

What is the use of updateone method in MongoDB?

Introduction to MongoDB updateOne () method The updateOne () method allows you to update a single document that satisfies a condition. The following shows the syntax of the updateOne () method: db.collection.updateOne (filter, update, options)

Is it too late to use MongoDB update one?

Too late but for newbies or students learning nodejs with mongodb. instead of updateOne method, just use the update method. Thanks for contributing an answer to Stack Overflow!

Where can I find the updateone and updatemany methods in Node JS?

C:\Users\ Your Name >node demo_update_many.js The updateOne () and the updateMany () methods return an object which contains information about how the execution affected the database.


Video Answer


1 Answers

Maybe you should use "$set" in your update query like this :

{$set: {"name": req.body.name}}, // Update 

More information in documentation

EDIT

If it doesn't work, this is probably because there is no match with your filter.

Maybe you should try to match with an ObjectId like this :

var ObjectID = require('mongodb').ObjectID;  // In your request { "_id": ObjectID(req.body._id)}, // Filter 

Hope it helps.

like image 175
Sparw Avatar answered Oct 04 '22 10:10

Sparw