Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose update or insert many documents

I'm trying to use the latest version of mongoose to insert an array of objects, or update if a corresponding product id already exists. I can't for the life of me figure out the right method to use (bulkWrite, updateMany etc) and I can't can't seem to figure out the syntax without getting errors. For example, i'm trying

Product.update({}, products, { upsert : true, multi : true }, (err, docs) => console.log(docs))

this throws the error DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead. MongoError: '$set' is empty. You must specify a field like so: {$set: {<field>: ...}

and using updateMany just gives me the $set error. I can't seem to find any examples of people using this method, just wondering if somebody can provide me with an example of how to properly use this.

To be clear, I generate an array of objects, then I want to insert them into my db, or update the entry if it already exists. The field I want to match on is called pid. Any tips or advice would be greatly appreciated!

EDIT:

My product schema

const productSchema = new mongoose.Schema({
  title : String,
  image : String,
  price_was : Number,
  price_current : {
    dollars : String,
    cents : String
  },
  price_save_percent : String,
  price_save_dollars : String,
  price_save_endtime : String,
  retailer : String
})

const Product = mongoose.model('Product', productSchema)

an example of the products array being passed

[
  {   
  title: 'SOME PRODUCT',
  image: '',
  price_was: '139.99',
  price_current: { dollars: '123', cents: '.49' },
  price_save_percent: '12%',
  price_save_dollars: '16.50',
  price_save_endtime: null,
  pid: 'VB78237321',
  url: '' 
  },
  { ... },
  { ... }
]
like image 313
Shan Robertson Avatar asked Feb 15 '19 17:02

Shan Robertson


People also ask

How can I update multiple documents in Mongoose?

Mongoose | updateMany() Function The updateMany() function is same as update(), except MongoDB will update all documents that match the filter. It is used when the user wants to update all documents according to the condition. Installation of mongoose module: You can visit the link to Install mongoose module.

What is Upsert true in Mongoose?

In MongoDB, an upsert means an update that inserts a new document if no document matches the filter . To upsert a document in Mongoose, you should set the upsert option to the Model.

What is true about Upserting a document?

Upsert with Operator Expressions: Or in other words, when the value of upsert option is true and no document matches the given filter, then the update operation inserts a new document in the given collection, and the fields inserted in this new document are the fields that specify in the query and update documents.

What does Mongoose insertMany return?

The insertMany() method returns a document that contains: The acknowledged key sets to true if operation executed with a write concern or false if the write concern was disabled.


1 Answers

You basically need bulkWrite operation

The array you want to update with

const products = [
  {   
    title: 'SOME PRODUCT',
    image: '',
    price_was: '139.99',
    price_current: { dollars: '123', cents: '.49' },
    price_save_percent: '12%',
    price_save_dollars: '16.50',
    price_save_endtime: null,
    pid: 'VB78237321',
    url: ''
  }
]

The query for bulk update

Model.bulkWrite(
  products.map((product) => 
    ({
      updateOne: {
        filter: { retailer : product.pid },
        update: { $set: product },
        upsert: true
      }
    })
  )
)
like image 77
Ashh Avatar answered Nov 09 '22 08:11

Ashh