不能将 $multiply 与 Number 一起使用 [英] Can't use $multiply with Number

查看:66
本文介绍了不能将 $multiply 与 Number 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Mongoose 时遇到更新问题,下面是架构定义.例如,下面我想通过将轮胎数量乘以 500 来更新汽车的价格:

I am running into an update problem using Mongoose, below is the schema definition. For example sake, below I would like to udpate the price of a car by multiplying the number of tires by 500:

car.js =

var mongoose = require('mongoose');

module.exports = mongoose.model('car', {
  
  make     : {type:String},
  model    : {type:String},
  num_tires: {type:Number, default:0}
  price    : {type:Number, default:0}
  
});
  

updateCost.js =

updateCost.js =

var Car = require('car');

Car.update(
  {make: 'Honda'},
  {price: {$multiply: ['$num_tires', 500]}},
  {multi: true},
  function(err) {
    console.log("Thar's an err", err);
  });

我收到的错误是:不能将 $multiply 与 Number 一起使用".

The error I am receiving is: "Can't use $multiply with Number".

有没有办法绕过模式定义 Number 来更新价格?感谢大家抽出时间.

Is there a way around the schema definition Number to update the price? Thanks everyone for their time.

推荐答案

您不能从 update() 中引用当前文档的属性.您要么必须遍历所有文档并更新它们,要么使用 $multiply 表达式作为 $project 管道内聚合中的算术运算来乘以 num_tires 字段与常量:

You cannot reference the current document's properties from within an update(). You'll either have to iterate through all the documents and update them or use aggregation with the $multiply expression as an arithmetic operation in aggregation within $project pipeline to multiply the num_tires field with the constant:

db.cars.aggregate([
     { 
         $match: { 
            make: 'Honda'
         } 
     },
     { 
          $project: { 
              make: 1, 
              model: 1, 
              num_tires: 1, 
              price: { 
                   $multiply: [ "$num_tires", 500 ] 
              } 
         } 
    }
]) 

或者您可以更新架构以包含任意字段 unit_price: {type: Number, default: 500} 然后您可以将其用作 $multiply: [ "$num_tires",$project 管道中的$unit_price"].

Or you can update your schema to include an arbitrary field unit_price: {type: Number, default: 500} which you can then use as $multiply: [ "$num_tires", "$unit_price" ] in the $project pipeline.

另一种选择是遍历所有匹配的文档并使用如下保存方法进行更新:

Another alternative is to iterate through all the matched documents and update using save method like this:

var Car = require('car');
Car.find({make: 'Honda'}).snapshot().forEach(
    function (e) {
        // update document, using its own properties
        e.price = e.num_tires * 500;

        // remove old property
        delete e.price;

        // save the updated document
        Car.save(e);
     }
);

或者使用 $set 操作符:

var Car = require('car');
Car.find().forEach(
    function (elem) {
        Car.update(
            {
                _id: elem._id,
                make: "Honda"
            },
            {
                $set: {
                    price: elem.num_tires * 500
                }
            },
            {multi: true},
            function(err) {
                 console.log("There's an error ", err);
            }
        );
    }
);

如果您有一个等于 num_tires 的价格默认值,那么您可能只想更新 price 字段而不引用同一字段中的另一个字段文档,使用 $mul 运算符:

If you had a default value for the price which is equal to the num_tires, then you may want to just update the price field without referencing another field in the same document, use the $mul operator:

var Car = require('car');
Car.update(
    {make: 'Honda'},
    {$mul: {price: 500}},
    {multi: true},
    function(err) {
       console.log("There's an error ", err);
    }
});

这篇关于不能将 $multiply 与 Number 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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