使用 node-cron 使用 mongoose 更新多个文档
Update multiple documents with mongoose using node-cron
我正在尝试使用 node-cron 使用 mongoose 更新多个文档。我想要完成的是创建的所有文档,比方说,7 月 1 日,在 30 天后将处于非活动状态。
我设法使用下面的代码更新多个文档,但我不知道如何更新多个包含日期小于当前日期的产品的文档:
我知道如何获取小于当前日期的产品列表,但我不知道如何将此逻辑应用到下面的代码中 -
let currentDate = new Date();
let query = Product.find({ dateCreated: {$lt: currentDate}});
Code is unrelated to what I'm trying to accomplish. This is a sample
code that will update multiple documents every 10 seconds.
const mongoose = require('mongoose');
const Product = require('../model/product');
const cron = require('node-cron');
cron.schedule('*/10 * * * * *', function(){
let productArray = ['Bacon', 'Apple', 'Fork', 'Beans'];
productArray.map(product => updateProduct(product));
});
let updateProduct = (name) => {
let randomNumber = Math.floor(Math.random() * (999 - 1 + 1));
let query = Product.findOne({name: name}).select({ 'name': 1 });
query.exec((err, product) => {
if(err) throw err;
if(product){
product.description = `Random description generate every 10 seconds: ${randomNumber}`;
product.save(err =>{
if(err) throw err;
console.log(`Successfully updated random description for: ${product.name}\n ${product.description}\n`);
});
}
});
};
Product Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const productSchema = mongoose.Schema({
name : String,
description : String,
status : String,
dateCreated : { type: Date, default: Date.now }, //
});
module.exports = mongoose.model( 'Product', productSchema );
这是我知道的在 mongoose 中更新多个文档的唯一方法。那么在使用 mongoose 和 node-cron 创建 30 天后,还有其他方法可以更新 mongoose 中的多个文档吗?如果我的问题令人困惑,请原谅我。
这里是完整版代码。
Here you no need to make multiple queries, like first selecting all
the products whose currentdate has matched your criteria. Cause that
you can do while updating the products itself.
const mongoose = require('mongoose');
const Product = require('../model/product');
const cron = require('node-cron');
cron.schedule('*/10 * * * * *', function() {
let productArray = ['Bacon', 'Apple', 'Fork', 'Beans'];
updateAllProducts(productArray, (err, res) => {
if (err)
//error handle, you can log here
else
//success handle, you can log here
})
});
let updateAllProducts = (productArray, callbackFn) => {
let randomNumber = Math.floor(Math.random() * (999 - 1 + 1));
description = `Random description generate every 10 seconds: ${randomNumber}`;
Product.update({
name: {
$in: productArray
},
dateCreated: {
$lt: currentDate
}
}, {
description: description
}, {
multi: true
}, (err, res) => {
if (err) {
callbackFn(err, null)
} else {
callbackFn(null, err);
}
});
};
在这种情况下,您还可以记录 Db 更新是失败还是成功。因此可以更好地控制代码。而不是在循环中进行多次调用。那将非常耗时且容易出错。
希望对您有所帮助!
我正在尝试使用 node-cron 使用 mongoose 更新多个文档。我想要完成的是创建的所有文档,比方说,7 月 1 日,在 30 天后将处于非活动状态。
我设法使用下面的代码更新多个文档,但我不知道如何更新多个包含日期小于当前日期的产品的文档:
我知道如何获取小于当前日期的产品列表,但我不知道如何将此逻辑应用到下面的代码中 -
let currentDate = new Date();
let query = Product.find({ dateCreated: {$lt: currentDate}});
Code is unrelated to what I'm trying to accomplish. This is a sample code that will update multiple documents every 10 seconds.
const mongoose = require('mongoose');
const Product = require('../model/product');
const cron = require('node-cron');
cron.schedule('*/10 * * * * *', function(){
let productArray = ['Bacon', 'Apple', 'Fork', 'Beans'];
productArray.map(product => updateProduct(product));
});
let updateProduct = (name) => {
let randomNumber = Math.floor(Math.random() * (999 - 1 + 1));
let query = Product.findOne({name: name}).select({ 'name': 1 });
query.exec((err, product) => {
if(err) throw err;
if(product){
product.description = `Random description generate every 10 seconds: ${randomNumber}`;
product.save(err =>{
if(err) throw err;
console.log(`Successfully updated random description for: ${product.name}\n ${product.description}\n`);
});
}
});
};
Product Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const productSchema = mongoose.Schema({
name : String,
description : String,
status : String,
dateCreated : { type: Date, default: Date.now }, //
});
module.exports = mongoose.model( 'Product', productSchema );
这是我知道的在 mongoose 中更新多个文档的唯一方法。那么在使用 mongoose 和 node-cron 创建 30 天后,还有其他方法可以更新 mongoose 中的多个文档吗?如果我的问题令人困惑,请原谅我。
这里是完整版代码。
Here you no need to make multiple queries, like first selecting all the products whose currentdate has matched your criteria. Cause that you can do while updating the products itself.
const mongoose = require('mongoose');
const Product = require('../model/product');
const cron = require('node-cron');
cron.schedule('*/10 * * * * *', function() {
let productArray = ['Bacon', 'Apple', 'Fork', 'Beans'];
updateAllProducts(productArray, (err, res) => {
if (err)
//error handle, you can log here
else
//success handle, you can log here
})
});
let updateAllProducts = (productArray, callbackFn) => {
let randomNumber = Math.floor(Math.random() * (999 - 1 + 1));
description = `Random description generate every 10 seconds: ${randomNumber}`;
Product.update({
name: {
$in: productArray
},
dateCreated: {
$lt: currentDate
}
}, {
description: description
}, {
multi: true
}, (err, res) => {
if (err) {
callbackFn(err, null)
} else {
callbackFn(null, err);
}
});
};
在这种情况下,您还可以记录 Db 更新是失败还是成功。因此可以更好地控制代码。而不是在循环中进行多次调用。那将非常耗时且容易出错。
希望对您有所帮助!