如何使用javascript承诺更新mongodb中的多个文档?
您好,我正在编写一个更新mongodb数据库的脚本。我的脚本执行以下操作如何使用javascript承诺更新mongodb中的多个文档?,javascript,node.js,mongodb,mongoose,promise,Javascript,Node.js,Mongodb,Mongoose,Promise,您好,我正在编写一个更新mongodb数据库的脚本。我的脚本执行以下操作 解析文件结构并为目录中的所有文件准备架构对象 然后它查询数据库并获取数据库中的文件文档 然后,我使用步骤1和步骤2中准备的数组进行循环,以找到合适的方法,即保存或更新 断开数据库的连接 由于mongodb API的异步特性,我不得不使用承诺。但我无法通过承诺实现这一点。下面是我的代码 // a array that stores the file Json objects var fileJsonArray = [];
// a array that stores the file Json objects
var fileJsonArray = [];
//passing the filePath twice as one serves as the source for the icons and the other is the base location.
//Check the documentaion for the file for more details
directoryParser.parseDirectory(...);
//console.log(fileJsonArray);
//connect to db
var mongoose = mongoose || require('mongoose');
mongoose.Promise = require('bluebird');
var dbUrl = 'mongodb://'+ config.dbLocation + ':' + config.dbPort + '/' + config.dbName;
mongoose.connect(dbUrl);
// an array that contains all the promises
var promises = [];
//console.log(fileJsonArray);
// get all the element
promises.push(File.find({}).exec()
.catch(function(err){
console.error('Error while getting all elements',err);
})
.then(function(err, allElements) {
if(err) {
console.error(err);
} else {
console.log(fileJsonArray);
for (var i = 0; i < fileJsonArray.length; i++) {
console.log(fileJsonArray[i].name);
if(fileJsonArray[i] in allElements) {
var query = {name : fileJsonArray[i].name, tags: fileJsonArray[i].tags}
, update = {size : fileJsonArray[i].size, bmarkedasdeleted: true}
, options = {multi: false};
promises.push(File.update(query, update, options).exec());
allElements.splice(allElements.indexOf(fileJsonArray[i]),1);
} else {
promises.push(fileJsonArray[i].save(function(err) {
if(err) {
console.error(fileJsonArray, err);
}
}));
}
}
for (var i = 0; i < allElements.length; i++) {
var query = {name : allElements[i].name, tags: allElements[i].tags}
, update = {bmarkedasdeleted: false}
, options = {multi: false};
promises.push(File.update(query, update, options).exec());
}
}
}));
//console.log(promises);
Promise.all(promises).then(function() {
mongoose.disconnect();
console.log('Disconnect Mongoose.');
}).catch(function(err){
console.log(err);
});
//存储文件Json对象的数组
var fileJsonArray=[];
//将文件路径传递两次,一个作为图标的源,另一个作为基本位置。
//有关更多详细信息,请查看该文件的文档
parseDirectory(…);
//log(fileJsonArray);
//连接到数据库
var mongoose=mongoose | | require('mongoose');
mongoose.Promise=require('bluebird');
var dbUrl='mongodb://'+config.dbLocation+':'+config.dbPort+'/'+config.dbName;
mongoose.connect(dbUrl);
//包含所有承诺的数组
var承诺=[];
//log(fileJsonArray);
//获取所有元素
promises.push(File.find({}).exec()
.catch(函数(err){
console.error('获取所有元素时出错',err);
})
.然后(功能(错误、等位基因){
如果(错误){
控制台错误(err);
}否则{
log(fileJsonArray);
for(var i=0;i
现在我无法保存任何值,因为find({}).exec()的then中的fileJsonArray为空
请告知。如果需要澄清,也要提出一些意见。
File.find({}).exec()
.然后((等位基因)=>{
var承诺=[];
for(var i=0;i{
mongoose.disconnect();
console.log('disconnectmongoose');
}).catch((错误)=>{
控制台日志(err);
});代码>
File.find({}).exec()
.然后((等位基因)=>{
var承诺=[];
for(var i=0;i{
mongoose.disconnect();
console.log('disconnectmongoose');
}).catch((错误)=>{
控制台日志(err);
});
只是不明白为什么要使用promissions.push(File.find({}).exec()
。应该使用File.find({}).exec()要获取所有元素,请使用promise.all并发执行。我这样做是因为它将帮助我收集数组中的所有承诺,然后我可以使用该数组调用承诺。all只是不明白为什么要使用promises.push(File.find({}).exec()
。应该使用File.find({}).exec()获取所有元素,然后使用promise