如何防止更新函数从 Meteor 插入到 MongoDB? [英] How to prevent update function insert to the MongoDB from Meteor?

查看:39
本文介绍了如何防止更新函数从 Meteor 插入到 MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于检查现有文档的 Meteor.method.当它没有找到它插入文档时,第二次它发现它更新并插入.请帮助检查并修复我的以下代码:

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" }

从代码来看,当我添加两次时,第二次,我丢失了 createdAt 字段.我不知道为什么.????

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;
},

推荐答案

我认为您正在寻找 Collection.upsert 方法.

I think you are looking for Collection.upsert method.

基本上它修改集合中的一个或多个文档,或者如果没有找到匹配的文档则插入一个.返回一个对象,键为 numberAffected(修改的文档数)和 insertId(插入的文档的唯一 _id,如果有).

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).

这篇关于如何防止更新函数从 Meteor 插入到 MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆