简体   繁体   English

UpdateMany不是函数MongoDB +节点

[英]UpdateMany Not a Function MongoDB + Node

I am writing a piece of code to be run on an npm-cron timer. 我正在编写一段要在npm-cron计时器上运行的代码。 Here it is: 这里是:

var CronJob = require('cron').CronJob;
var job = new CronJob({
  cronTime: '* * * * *',
  onTick: function() {
    var monk = require('monk'); // persistence module over MongoDB
    var db = monk('localhost:27017/test');

    console.log("Hello from the Cron job! " + new Date());
    var collection = db.get('Activity');

    collection.updateMany({"repetitions.today": {$gt: 0}},
    {
        $set: {
            "repetitions.today": 0,
        }
    }, function(err, activity){
        if (err) throw err;
    });


  },
  start: true,
  timeZone: 'America/Los_Angeles'
});
job.start();

The problem is that I get an error on the collection.updateMany() line. 问题是在collection.updateMany()行上出现错误。 The error states that 错误指出

updateMany is not a function updateMany不是函数

On the other hand, if i use update() instead of updateMany() , the code works (but only on the first item in Mongo collection, as expected). 另一方面,如果我使用update()而不是updateMany() ,则代码可以正常工作(但仅在Mongo集合的第一项上起作用,如预期的那样)。 What am I doing wrong and what could be causing this? 我在做什么错,可能是什么原因造成的?

I tried re-writing this using foreach() but it is also not recognized as a function. 我尝试使用foreach()重写它,但它也未被识别为函数。

Hmm.... 嗯...

Might want to try something like this (ES6) 可能想尝试这样的事情(ES6)

db.collection('Activity').updateMany({"repetitions.today": {$gt: 0}},
{
    $set: { "repetitions.today": 0 }
}).then(r => {
    console.log('Success!');
}).catch(err => {
    console.log('Error - ' + err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM