Mongoose 更新或插入多个文档 [英] Mongoose update or insert many documents

查看:116
本文介绍了Mongoose 更新或插入多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用最新版本的 mongoose 插入一组对象,或者在相应的产品 ID 已存在时进行更新.我一生都无法找出正确的使用方法(bulkWrite、updateMany 等),而且我似乎无法在不出错的情况下弄清楚语法.例如,我正在尝试

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

这会引发错误 DeprecationWarning: collection.update is deprecated.请改用 updateOne、updateMany 或 bulkWrite.MongoError: '$set' 为空.您必须像这样指定一个字段:{$set: {: ...}

并且使用 updateMany 只会给我 $set 错误.我似乎找不到任何使用这种方法的人的例子,只是想知道是否有人可以为我提供一个如何正确使用它的例子.

明确地说,我生成了一个对象数组,然后我想将它们插入到我的数据库中,或者更新条目(如果它已经存在).我要匹配的字段称为 pid.任何提示或建议将不胜感激!

我的产品架构

const productSchema = new mongoose.Schema({标题:字符串,图像:字符串,price_was :数字,价格当前:{美元:字符串,分:字符串},price_save_percent :字符串,price_save_dollars :字符串,price_save_endtime :字符串,零售商:字符串})const Product = mongoose.model('Product', productSchema)

传递的 products 数组示例

<预><代码>[{title: '一些产品',图像: '',price_was: '139.99',price_current: { 美元: '123', 美分: '.49' },price_save_percent: '12%',price_save_dollars: '16.50',price_save_endtime:空,pid: 'VB78237321',网址:''},{ ... },{ ... }]

解决方案

你基本上需要 bulkWrite 操作

要更新的数组

const 产品 = [{title: '一些产品',图像: '',price_was: '139.99',price_current: { 美元: '123', 美分: '.49' },price_save_percent: '12%',price_save_dollars: '16.50',price_save_endtime:空,pid: 'VB78237321',网址:''}]

批量更新查询

Model.bulkWrite(products.map((product) =>({更新一:{过滤器:{零售商:product.pid},更新:{ $set:产品},upsert:真的}})))

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: '' 
  },
  { ... },
  { ... }
]

解决方案

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
      }
    })
  )
)

这篇关于Mongoose 更新或插入多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆