My app should update if tmx is newer, if older do nothing and if doesn't exist insert the document. If the document is inserted, it works perfectly, else it doesn't update properly or says E11000 dup key. trying to figure out if my callback are wrong or the logic. (I'm new to node.js+mongodb) MongoClient = require('mongodb').MongoClient, assert = require('assert'), url = 'mongodb://localhost:27017/pfc';
MongoClient.connect(url, function (err, db) {
run(db);
});
function run(db) {
fs.readFile('log.log', 'utf8', function (err, source) {
if (err) throw err;
var dataFile = JSON.parse(source);
dataFile.forEach(function (item) {
upsert(db, item, function (err, result) {
if (err) console.dir(err);
});
});
})
}
function upsert(db, doc, callback) {
db.collection('flags').findOne({vid: doc.vid}, function (err, item, result) {
if (item.vid != null) {
if (!(item.tmx instanceof Date)) {
item.tmx = new Date(item.tmx)
}
if(!(doc.tmx instanceof Date)){
doc.tmx = new Date(doc.tmx)
}
if (item.tmx < doc.tmx) {
console.dir("Date validation")
db.collection('flags').updateOne({vid: item.vid}, {
$set: {
"tmx": doc.tmx
}
},{upsert:true}, function (err, result) {
callback(err, result);
}
)
callback(err, result);
}
else{
console.dir("older")
callback(err, result);
}
}
else {
db.collection('flags').insertOne(doc, function(err, result) {
callback(err, result);
});
}
})}
Edit: The documents from the 'log.log' file have this structure:
{ vid:2848 tmx: "2015-07-18T23:56:17.000Z" }
{ vid: 2848 tmx: 2015-07-19T00:00:17.000Z }
collection.find({vid: doc.vid},function(err,item){
if(!item) // didnt find in collection, items with vid: 2848 insert doc to collection else if(item) //found an item with vid:2848 if (item.tmx < doc.tmx)//only update if doc.tmx is newer update collection with the most recent document
with @Aaron Dufour help I got rid of the callback problem, thanks :) but now the problem is when I have the collection already populated and go look for newest documents in log.log, it starts from the oldest document till the newest again :(
vidvalue?