Home > Mobile >  Mongoose Update document
Mongoose Update document

Time:10-24

I am using Node.js and mongoose Module, I am looking for a way to count how many documents a user has and then if they have more then 0 documents it would edit their existing document and add the input so at the end it would have the previous text the text that the user sent, so far this is how much I gotten.

const List = require('../Models/list.js')
List.countDocuments({}, function(err, count) {
     if(count>0){
//edit document
} 
     else if(count=0){
const input = List.create({
          User: User.name,
          Songlist: args[0],
        })
    }

})
console.log('done')

here is how I think the code would look like

  List.update(User.name) => update Songlist into List.Songlist   '|'   args[0]

CodePudding user response:

I have never seen an update method like that. I am a nodejs developer. well, maybe there's a way like that.

Here's how I do to update a document

await Product.findByIdAndUpdate(id, //here where I have written "id" you have to write the id of the document you want to update. { //here in this object you have to put the variables of updated values title: title, description:description, product_id:product_id, category, price, });

there is also another method

await Product.findOneAndUpdate(name: 'asim', //let's suppose
{ //updated values
title:title,product: product
})
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

you can also read the documentation here https://mongoosejs.com/docs/tutorials/findoneandupdate.html

  • Related