Reputation: 1104
I have the Meteor.method that check the existing document. when it does not found it insert the document, for second time it found it update and insert. Please help to check and fix my following code:
'upload': function upload(data, name, eventId) {
const wb = XLSX.read(data, {type:'binary'});
var checkUpdate;
XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]).forEach(r => {
r.owner = this.userId,
r.username = Meteor.user().username,
r.updatedAt = new Date(),
r.amount = Number(r.amount),
r.eventid = eventId,
r.firstname = r.firstname.trim(),
r.lastname = r.lastname.trim(),
Registers.findOne({ firstname: r.firstname, lastname: r.lastname, eventid: eventId }) ?
Registers.update({ firstname: r.firstname, lastname: r.lastname, eventid: eventId }, { $set: {updatedAt: r.updatedAt, amount: r.amount } })
:
r.createdAt = new Date(),
Registers.insert(r)
})
return wb;
},
First time, if the database is empty, it insert new document. For second time, if it found the document it then update the document, and also insert new document with update function not insert function.
meteor:PRIMARY> db.registers.find({ eventid: "aZrumf45q8sBGGrY2" })
{ "_id" : "MzqD73vsgyxRTyekJ", "salution" : "Mr.", "firstname" : "qwer", "lastname" : "asdf", "gender" : "Male", "age" : "38", "province" : "chiangmai", "amount" : 1000, "owner" : "rBjWm4PRTHwAo2vRS", "username" : "mai", "updatedAt" : ISODate("2017-09-11T12:28:36.966Z"), "eventid" : "aZrumf45q8sBGGrY2", "createdAt" : ISODate("2017-09-11T12:20:49.731Z") }
{ "_id" : "suzPhYkvQQcYjZj5p", "salution" : "Mr.", "firstname" : "abcd", "lastname" : "efgh", "gender" : "Male", "age" : "30", "province" : "chiangmai", "amount" : 500, "owner" : "rBjWm4PRTHwAo2vRS", "username" : "mai", "updatedAt" : ISODate("2017-09-11T12:28:37.017Z"), "eventid" : "aZrumf45q8sBGGrY2", "createdAt" : ISODate("2017-09-11T12:20:49.739Z") }
{ "_id" : "QYgF7aLvBDwo5amuA", "salution" : "Mr.", "firstname" : "qwer", "lastname" : "asdf", "gender" : "Male", "age" : "38", "province" : "chiangmai", "amount" : 1000, "owner" : "rBjWm4PRTHwAo2vRS", "username" : "mai", "updatedAt" : ISODate("2017-09-11T12:28:36.966Z"), "eventid" : "aZrumf45q8sBGGrY2" }
{ "_id" : "XYSBxgiz5T9QXad6r", "salution" : "Mr.", "firstname" : "abcd", "lastname" : "efgh", "gender" : "Male", "age" : "30", "province" : "chiangmai", "amount" : 500, "owner" : "rBjWm4PRTHwAo2vRS", "username" : "mai", "updatedAt" : ISODate("2017-09-11T12:28:37.017Z"), "eventid" : "aZrumf45q8sBGGrY2" }
From the code, when I add twice, the second time, I lose createdAt field. I don't know why.????
I have got it !!!! Thank you very much for all comments
'upload': function upload(data, name, eventId) {
const wb = XLSX.read(data, {type:'binary'});
var checkUpdate;
XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]).forEach(r => {
if (!Registers.findOne({ firstname: r.firstname, lastname: r.lastname, eventid: eventId })) {
r.owner = this.userId,
r.username = Meteor.user().username,
r.updatedAt = new Date(),
r.amount = Number(r.amount),
r.eventid = eventId,
r.firstname = r.firstname.trim(),
r.lastname = r.lastname.trim(),
r.createdAt = new Date(),
Registers.insert(r)
} else {
r.owner = this.userId,
r.username = Meteor.user().username,
r.updatedAt = new Date(),
r.amount = Number(r.amount),
r.eventid = eventId,
r.firstname = r.firstname.trim(),
r.lastname = r.lastname.trim(),
Registers.update({ firstname: r.firstname, lastname: r.lastname, eventid: eventId }, { $set: {updatedAt: r.updatedAt, amount: r.amount } })
}
})
return wb;
},
Upvotes: 1
Views: 108
Reputation: 6018
I think you are looking for Collection.upsert method.
Basically it modifies one or more documents in the collection, or insert one if no matching documents were found. Returns an object with keys numberAffected (the number of documents modified) and insertedId (the unique _id of the document that was inserted, if any).
Upvotes: 1