gpt4 book ai didi

javascript - 如何更新 MongoCollection 但保留一些值?

转载 作者:行者123 更新时间:2023-12-03 04:41:29 25 4
gpt4 key购买 nike

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

因此,当用户单击按钮时,我尝试在我的 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);
},

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

感谢您的帮助

最佳答案

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

基本上,您想要 lockTheMachine()函数 upsert 仅设置 nameMachine , isUsed ,和whoUseIt字段,您只需要 upsertCollectionMachines()设置stateMachine , ipAddr , port , protocol更新时的字段且仅设置 isUsedwhoUseIt插入时使用默认值?

您应该能够使用以下更新插入来做到这一点。

在您的 upsertCollectionMachines() 中使用此更新插入功能。

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

它将始终设置 stateMachine , ipAddr , port , protocol字段无论是插入还是更新,都只会修改isUsedwhoUseIt如果正在插入(因此,它不会覆盖其他函数设置的值)。

在您的 lockTheMachine() 中使用此更新插入功能。

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

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

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

The fields and values of both the <query> and <update> parameters if the <update> parameter contains update operator expressions

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

关于javascript - 如何更新 MongoCollection 但保留一些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43069680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com