如何使用 mongoose 或 mongodb 查询更改架构内方案数组内的值?
How can I Change value inside array of schemes inside schema with mongoose or mongodb query.?
我有以下架构类型:
派特网:
var PaitentSchema = new mongoose.Schema({
username: String,
password: String,
name: String,
protocol: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Protocol'
},
treatmentTypes: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'TreatmentType'
}],
accesses: [AccessSchema],
reports: [ReportSchema],
}, { collection: ' Paitents' });
和 AccessSchema:
var AccessSchema = new mongoose.Schema({
stageBool: Boolean,
exerciseBool: [{ type: Boolean }]
});
我想要做的是 更新 exerciseBool 数组,例如将数组中的一个值从 'false' 更改为 'true'.
我已经尝试过这段代码及其对我的工作,但问题是我从客户端获取索引,所以我需要以 动态方式 嵌入索引(不总是 0 和 1)
这是我所做的(不是动态的):
const paitent = await Paitent.updateOne({ username: req.params.username },
{ $set: { "accesses.0.exerciseBool.1": true } });
我想做这样的事情,但是是以动态索引的方式。
请有人可以帮助我吗?
谢谢。
如果您使用的是 MongoDB 版本 >= 4.4。您可以使用 $function along with update-with-aggregation-pipeline 动态更新数组。试试这个:
let index1 = 0;
let index2 = 1;
db.patients.updateOne(
{ username: "dhee" },
[
{
$set: {
accesses: {
$function: {
body: function(accesses, index1, index2) {
if (accesses[index1] != null
&& accesses[index1].exerciseBool[index2] != null) {
accesses[index1].exerciseBool[index2] = true;
}
return accesses;
},
args: ["$accesses", index1, index2],
lang: "js"
}
}
}
}
]
);
如您所说,索引是已知的,但值可能会改变。
您可以使用以下内容创建查询。
const accessesIndex = 0;
const exerciseBoolIndex = 1;
const update = { $set: { [`accesses.${accessesIndex}.exerciseBool.${exerciseBoolIndex}`]: true } };
console.log(update);
//const paitent = await Paitent.updateOne({ username: req.params.username }, update); // run your update query like this
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
更新
检查索引是否存在然后只更新记录。
添加到您的查询中 "accesses.0.exerciseBool.1": { $exists: true }
以确保 accesses.0.exerciseBool.1
存在于记录中。
const accessesIndex = 0;
const exerciseBoolIndex = 1;
const username = 'abc';
const key = `accesses.${accessesIndex}.exerciseBool.${exerciseBoolIndex}`;
const query = { username, [key]: { "$exists": true } };
console.log('query:', query);
const update = { $set: { [key]: true } };
console.log('update:', update);
我有以下架构类型: 派特网:
var PaitentSchema = new mongoose.Schema({
username: String,
password: String,
name: String,
protocol: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Protocol'
},
treatmentTypes: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'TreatmentType'
}],
accesses: [AccessSchema],
reports: [ReportSchema],
}, { collection: ' Paitents' });
和 AccessSchema:
var AccessSchema = new mongoose.Schema({
stageBool: Boolean,
exerciseBool: [{ type: Boolean }]
});
我想要做的是 更新 exerciseBool 数组,例如将数组中的一个值从 'false' 更改为 'true'. 我已经尝试过这段代码及其对我的工作,但问题是我从客户端获取索引,所以我需要以 动态方式 嵌入索引(不总是 0 和 1)
这是我所做的(不是动态的):
const paitent = await Paitent.updateOne({ username: req.params.username },
{ $set: { "accesses.0.exerciseBool.1": true } });
我想做这样的事情,但是是以动态索引的方式。 请有人可以帮助我吗? 谢谢。
如果您使用的是 MongoDB 版本 >= 4.4。您可以使用 $function along with update-with-aggregation-pipeline 动态更新数组。试试这个:
let index1 = 0;
let index2 = 1;
db.patients.updateOne(
{ username: "dhee" },
[
{
$set: {
accesses: {
$function: {
body: function(accesses, index1, index2) {
if (accesses[index1] != null
&& accesses[index1].exerciseBool[index2] != null) {
accesses[index1].exerciseBool[index2] = true;
}
return accesses;
},
args: ["$accesses", index1, index2],
lang: "js"
}
}
}
}
]
);
如您所说,索引是已知的,但值可能会改变。
您可以使用以下内容创建查询。
const accessesIndex = 0;
const exerciseBoolIndex = 1;
const update = { $set: { [`accesses.${accessesIndex}.exerciseBool.${exerciseBoolIndex}`]: true } };
console.log(update);
//const paitent = await Paitent.updateOne({ username: req.params.username }, update); // run your update query like this
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
更新
检查索引是否存在然后只更新记录。
添加到您的查询中 "accesses.0.exerciseBool.1": { $exists: true }
以确保 accesses.0.exerciseBool.1
存在于记录中。
const accessesIndex = 0;
const exerciseBoolIndex = 1;
const username = 'abc';
const key = `accesses.${accessesIndex}.exerciseBool.${exerciseBoolIndex}`;
const query = { username, [key]: { "$exists": true } };
console.log('query:', query);
const update = { $set: { [key]: true } };
console.log('update:', update);