使用 Mongoose 批量更新 [英] Batch update with Mongoose
问题描述
我正在从 RETS(XML) 提要中提取数据,并使用 node 和 mongoose 将其保存在本地 MongoDB 中.
I'm pulling data from a RETS(XML) feed and saving it in a local MongoDB using node and mongoose.
我需要定期更新文档并删除不活动的文档以及添加新文档.我没有对 Mongo 或 RETS 服务器进行多次查询,而是同时拉取并循环访问数据.
Periodically I need to update the documents and delete the inactive ones as well as add new ones. Rather than making multiple queries to Mongo or the RETS server, I was pulling both and looping through the data.
这工作正常,但有没有办法通过更新和插入将 Mongoose 结果保存回数据库?还是我需要找到每个文档并单独更新?
This works fine but is there a way to save the Mongoose results back to the database with updates and inserts? Or do I need to find each document and update it individually?
推荐答案
在 MongoDB 上,要使用 Mongoose 更新多个文档(不仅仅是一个),您可以使用 multi 选项:
On MongoDB, to update multiple documents (not just one) using Mongoose you can use the multi option:
Model.updateMany({
size: 'lage'
}, {
$set: { size: 'large' }
});
See more on in the Mongoose documentation for updating documents and here
这篇关于使用 Mongoose 批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!