MongoDB有一个非常奇怪的问题,在49个findOne()和save()后,我的集合完全被锁定,即使重新启动我的应用程序也无法再进行查询。
app.js:
var fs = require('fs');
var async = require('async');
var mongoose = require('mongoose');
mongoose.connect('127.0.0.1:27017/locked-db-test1');
var Product = require('./models/product.js');
var crawlArray = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
var count = 0;
async.eachSeries(crawlArray, function(crawl, callback) {
if (crawl.productId !== undefined) {
var product = new Product();
product.idProduct = crawl.productId;
console.log('BEFORE FIND: ' + product.idProduct);
Product.findOne({idProduct: product.idProduct}, function(err, productBSON) {
console.log('FIND');
if (err) {
console.error(err);
count++;
callback();
}
else if (productBSON === null) {
console.log('NEW PRODUCT: ' + product.idProduct);
product.save(function(err) {
console.log('SAVED PRODUCT: ' + product.idProduct);
if (err) {
console.error(err);
}
count++;
console.log(crawlArray.length + ' ' + count);
callback();
});
}
else {
console.log(productBSON);
count++;
callback();
}
});
}
else {
count++;
callback();
}
}, function(err) {
console.log('*** END ***');
});
./模型/ product.js:
var mongoose = require('mongoose');
var productSchema = mongoose.Schema({
title: {type: String},
url: {type: String},
description: {type: String},
images: {type: [String]},
size: {type: [String]},
dominantColor: {type: [Number]},
availability: {type: Boolean},
lastCrawlTimeUTC: {type: Number},
category: {type: String},
price: {type: Number},
regularPrice: {type: Number},
designer: {type: String},
merchant: {type: String},
offer: {type: Number},
idProduct: {type: String},
idMerchant: {type: String},
isPresent: {type: Boolean}
});
productSchema.methods.init = function(crawl) {
};
productSchema.methods.setMerchantInfo = function(crawl, crawlname) {
};
productSchema.methods.setImages = function(crawl) {
};
productSchema.methods.setDominantColor = function(dominantColor) {
};
module.exports = mongoose.model('Product', productSchema);
data.json: https://www.dropbox.com/s/ttwxq0ez0ovqxxt/data.json?dl=0
您将需要此命令来删除集合:
mongo locked-db-test1 --eval "db.dropDatabase()"
****更新****
如果我删除了productSchema的所有方法或一半或属性,它的工作正常。为什么会改变什么?
感谢您的帮助。
答案 0 :(得分:0)
当您在完成后重新使用/关闭它们时,听起来像是为每个查询打开了与服务器的新连接。