【问题标题】:How to upsert MongoCollection but keep some values?如何更新 MongoCollection 但保留一些值?
【发布时间】:2017-08-21 12:49:36
【问题描述】:

嗨, 我不知道如何给它起一个好的标题,但我相信你会理解的

所以我试图在我的 Mongo 集合上设置 2 个字段,当用户点击一个按钮但每 60sc 时我有一个正在运行的方法,并且是谁做的:

  upsertCollectionMachines = Meteor.bindEnvironment(function() {
      machinesCount = 0;
        var protocol;
        var port;
        Machine.list({
            inspect: true
        }, Meteor.bindEnvironment(function(err, machines) {
            if (typeof machines === "undefined") {
                console.error("Impossible to access 'machines' from 'Machine.list:  '" + err);
            } else {
                machines.forEach(Meteor.bindEnvironment(function(machineInfo) {
                    machinesCount += 1;
                    if (machineInfo.url != null) {
                        var urlToArray = machineInfo.url.split(":")
                        port = urlToArray[urlToArray.length - 1];
                        protocol = "https";
                    } else {
                        port = "2376";
                        protocol = "https";
                    }
                    InfosMachines.upsert({
                        nameMachine: machineInfo.name,
                    }, {
                        nameMachine: machineInfo.name,
                        stateMachine: machineInfo.state,
                        ipAddr: machineInfo.driver.ipAddress,
                        port: port,
                        protocol: protocol
//here I want to add isUsed: false,  
//whoUseIt: "", but I think if I set isUsed true --> after 60s it will be false again right ?
                    });
                }));
                if (apiKeyGenerated == false) {
                    getTheAPIKeyGrafana(function(err, res) {
                        if (!err) {
                            apiKeyGenerated = true;
                            updateDashboard();
                        } else {
                            console.error(err);
                        }
                    });
                }else{
                  updateDashboard();
                }
            }
        }));
    })

重要的部分是InfosMachines.upsert()。现在我想设置 2 个这样的字段:

'lockTheMachine': function(nameMachine, nameUser){
        InfosMachines.upsert({
            nameMachine: nameMachine,
        }, {
            nameMachine: nameMachine,
            isUsed: true,
            whoUseIt: nameUser
        });
        //upsertCollectionMachines();
        console.log(nameUser +" has locked the machine: " + nameMachine);
        },

但首先它看起来不像将 2fields 添加到集合中,其次 60s 间隔方法会删除它们对吗?

感谢您的帮助

【问题讨论】:

  • 我有一个想法,我会打开这个帖子,但我的想法是为二手机器创建第二个集合
  • 嗨杰罗姆;当您说它看起来不像添加 2 个字段时,您能否准确说明您对数据的预期结果以及实际更新的内容?
  • @VinceBowdren 我明天做,我戳你

标签: javascript mongodb meteor collections


【解决方案1】:

在你的问题中准确地理解你想要做什么有点困难,但我想我明白了它的主要要点。

基本上,您希望 lockTheMachine() 函数 upsert 只设置 nameMachineisUsedwhoUseIt 字段,并且您只希望 upsertCollectionMachines() 设置 stateMachineipAddrport , protocol 字段在更新时仅将 isUsedwhoUseIt 设置为插入时的默认值?

您应该能够使用以下 upserts 来做到这一点。

在你的 upsertCollectionMachines() 函数中使用这个 upsert。

InfosMachines.upsert({
  nameMachine: machineInfo.name,
}, {
  $set: {
    stateMachine: machineInfo.state,
    ipAddr: machineInfo.driver.ipAddress,
    port: port,
    protocol: protocol
  },
  $setOnInsert: {
    isUsed: false,
    whoUseIt: "",
  }
});

无论是插入还是更新,它都会设置stateMachineipAddrportprotocol字段,但只会修改isUsedwhoUseIt如果它正在插入(因此,它不会覆盖从其他函数设置的值)。

在你的 lockTheMachine() 函数中使用这个 upsert。

InfosMachines.upsert({
  nameMachine: nameMachine,
}, {
  $set: {
    isUsed: true,
    whoUseIt: nameUser
  }
});

它只会插入或更新这 2 个特定字段(isUsedwhoUseIt)。

现在,为什么会这样?首先,我们使用update operator expressions(而不仅仅是field:value 表达式),它允许您仅在需要时更新特定字段,并且使用$setOnInsert 确保我们只在插入时修改这些字段。请注意,如果 upsert 确定它需要插入一个新文档,根据 mongodb upsert option behavior,它会使用...创建一个新文档。

如果<update> 参数包含更新运算符表达式,则<query><update> 参数的字段和值

这意味着您实际上不必在更新表达式中明确地 $set nameMachine 字段。

【讨论】:

  • 嗨,首先谢谢你,这是我得到的最好的答案!你的解释非常棒!该系列已按我的意愿进行更新,因此非常完美:)
猜你喜欢
  • 1970-01-01
  • 2018-01-03
  • 2018-08-15
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多