具有带有记录的集合,需要将列的布尔值转换为字符串:
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
passed_phd: true,
age: 55,
location: "texas",
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
passed_phd: true,
location: "texas",
},
{
_id: "bmasndvhjbca",
name: "stuart",
occupation: "lab assistant",
age: 25,
passed_phd: false,
location: "texas",
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
passed_phd: false,
location: "texas"
}
]
如何将记录的布尔值更改为字符串。
passed_phd中具有true(boolean)的值应转换为“ yes”(string)
passed_phd中具有false(boolean)的值应转换为“ no”(string)
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
passed_phd: "yes",
age: 55,
location: "texas",
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
passed_phd: "yes",
location: "texas",
},
{
_id: "bmasndvhjbca",
name: "stuart",
occupation: "lab assistant",
age: 25,
passed_phd: "no",
location: "texas",
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
passed_phd: "no",
location: "texas"
}
]
mongodb 4.0版
尝试过此:
{passed_phd : [{'passed_phd':true},{$set:{'passed_phd':"yes"}] }
错误:预期为“ [”或AggregationStage,但找到“ {”。
您需要使用$toString,请尝试在以下阶段添加:
{
$addFields: {
passed_phd: {
$toString: "$passed_phd"
}
}
}