.updateOne on MongoDB not working in Node.js

后端 未结 8 993
迷失自我
迷失自我 2021-01-03 20:07

I have the following code:

connection((db) => {
            db.collection(\'orders\')
                .updateOne(
                    { \"_id\": req.body         


        
相关标签:
8条回答
  • 2021-01-03 20:30

    I had this trouble too, I create a constant with _id from req.body

    const {_id} = req.body  //pass ID user
    await User.updateOne({_id},{$set:data}, (err)=>{
                if(err) return res.status(200).json({
                    error: true,
                    code: 115,
                    message: "Erro to update user!"
                })
            })
    
    0 讨论(0)
  • 2021-01-03 20:34

    Too late but for newbies or students learning nodejs with mongodb. instead of updateOne method, just use the update method.

    connection((db) => {
              db.collection('orders')
                   .update(
                      { "_id": req.body._id}, // Filter
                      {$set: {"name": req.body.name}}, // Update
                      {upsert: true} // add document with req.body._id if not exists 
    
                 )
                .then((obj) => {
                   console.log('Updated - ' + obj);
                  res.redirect('orders')
             })
            .catch((err) => {
               console.log('Error: ' + err);
          }) })
    
    0 讨论(0)
  • 2021-01-03 20:40

    The correct syntax is:

    monDb.collection.updateOne(
        {"_id": ObjectID(req.params.id)}, 
        { $set: updateDoc }, 
        function(err, doc) {
          ...
        }
    );
    
    0 讨论(0)
  • 2021-01-03 20:43

    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.

    0 讨论(0)
  • 2021-01-03 20:44

    Use {$set: {"name": req.body.name}} (as Sparw mentioned) to update the the properties you want in the document. Also, if the id that you pass as a parameter does not exists in the collection (and you want to create one with the same id) you can pass as a third parameter {upsert: true} to create one.

    In your example:

    connection((db) => {
              db.collection('orders')
                   .updateOne(
                      { "_id": req.body._id}, // Filter
                      {$set: {"name": req.body.name}}, // Update
                      {upsert: true} // add document with req.body._id if not exists 
    
                 )
                .then((obj) => {
                   console.log('Updated - ' + obj);
                  res.redirect('orders')
             })
            .catch((err) => {
               console.log('Error: ' + err);
          }) })
    
    0 讨论(0)
  • 2021-01-03 20:48

    For me, I have to delete the "_id"/id field before passing the object in the update.

    Or it will say that the field is invalid.

    Obviously, updated the key while you're using it as a reference isn't the best thing to do.

    0 讨论(0)
提交回复
热议问题