我非常需要有关在MongoDB中更新文档的帮助。
这是我的问题。我需要删除记录数组中类型为OLD的对象,但是在删除它之前,我需要将其值添加到类型为CURRENT的对象中。因此,给出以下示例。查询应将CURRENT对象的值更改为120,并且将删除类型为OLD的对象;
我无法进行两次查询,因为此查询将更新数千个数据。
这在node.js中使用猫鼬。
谢谢你!
{
name: 'Test',
records: [
{
type: 'NEW',
value: 50,
},
{
type: 'OLD',
value: 100,
},
{
type: 'CURRENT',
value: 20
}
]
}
答案 0 :(得分:-1)
您可以使用MongoDB v4.2中的update with aggregation pipeline,
document.getElementById("box1").onchange = function(){
document.getElementById("box2").value = this.value;
};
List<Class> klasses = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper();
JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper);
klasses.forEach(klass -> {
try {
JsonSchema jsonSchema = schemaGenerator.generateSchema(klass);
String schema = JSONUtils.toPrettyJSON(jsonSchema);
File file = new File("javascript/external/packages/types/res/" + klass.getSimpleName() + ".schema");
Files.write(file.toPath(), schema.getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
与CURRENT
类型的值和$set
类型的值求和CURRENT
删除类型OLD
$set