- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我不知道如何为它起一个好的标题,但我相信你会理解的
因此,当用户单击按钮时,我尝试在我的 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
更新时的字段且仅设置 isUsed
和whoUseIt
插入时使用默认值?
您应该能够使用以下更新插入来做到这一点。
在您的 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
字段无论是插入还是更新,都只会修改isUsed
和whoUseIt
如果正在插入(因此,它不会覆盖其他函数设置的值)。
在您的 lockTheMachine()
中使用此更新插入功能。
InfosMachines.upsert({
nameMachine: nameMachine,
}, {
$set: {
isUsed: true,
whoUseIt: nameUser
}
});
它只会插入或更新这 2 个特定字段( isUsed
和 whoUseIt
)。
现在,为什么这会起作用?首先,我们使用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/
我是一名优秀的程序员,十分优秀!