如何将布尔转换为字符串&;按聚合管道中的条件更改值-MongoDB
有记录的集合,需要将列的布尔值转换为字符串:如何将布尔转换为字符串&;按聚合管道中的条件更改值-MongoDB,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,有记录的集合,需要将列的布尔值转换为字符串: [ { _id: "bmasndvhjbcw", name: "lucas", occupation: "scientist", passed_phd: true, age: 55, location: "texas", }, { _id: "bmasndvhjbcx", name: "mark", occupation: "scientist", age:
[
{
_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"
}
]
如何将记录的布尔值更改为字符串
[
{
_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"
}
]
传递的参数中为true(布尔)的值应转换为“是”(字符串)
[
{
_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"
}
]
传递的phd中包含false(布尔值)的值应转换为“否”(字符串)
[
{
_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"
}
]
mongodb版本4.0
[
{
_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"
}
]
尝试过这个:
[
{
_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 : [{'passed_phd':true},{$set:{'passed_phd':"yes"}] }
Expected "[" or AggregationStage but "{" found.
错误:
[
{
_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 : [{'passed_phd':true},{$set:{'passed_phd':"yes"}] }
Expected "[" or AggregationStage but "{" found.
您需要使用转换为字符串和条件运算符来更改字段值,请尝试将以下阶段添加到聚合管道:
[
{
_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"
}
]
{
$addFields: {
passed_phd: {
$switch: {
branches: [
{
case: {
$eq: [
"$passed_phd",
true
]
},
then: "yes"
},
{
case: {
$eq: [
"$passed_phd",
false
]
},
then: "no"
},
],
default: {
$toString: "$passed_phd"
}
}
}
}
}
测试:
[
{
_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"
}
]
如果字段存在,则无需使用$switch
,因为已通过的字段只能有两个值,所以只需使用$cond
运算符:
[
{
_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"
}
]
{
$addFields: {
passed_phd: {
$cond: [
{
$eq: [
"$passed_phd",
true
]
},
"yes",
"no"
]
}
}
}
测试:您需要使用转换为字符串和条件运算符来更改字段值,请尝试将以下阶段添加到聚合管道中:
[
{
_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"
}
]
{
$addFields: {
passed_phd: {
$switch: {
branches: [
{
case: {
$eq: [
"$passed_phd",
true
]
},
then: "yes"
},
{
case: {
$eq: [
"$passed_phd",
false
]
},
then: "no"
},
],
default: {
$toString: "$passed_phd"
}
}
}
}
}
测试:
[
{
_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"
}
]
如果字段存在,则无需使用$switch
,因为已通过的字段只能有两个值,所以只需使用$cond
运算符:
[
{
_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"
}
]
{
$addFields: {
passed_phd: {
$cond: [
{
$eq: [
"$passed_phd",
true
]
},
"yes",
"no"
]
}
}
}
测试:您必须使用$project
或addFields
阶段,并使用$cond
或$switch
运算符转换值。另外,请发布到目前为止您尝试过的聚合查询。尝试此{passed_phd:[{'passed_phd':true},{$set:{'passed_phd':“yes”}
在聚合管道中,您必须使用$project
或addFields
阶段,并使用$cond
或$switch
运算符来转换值。此外,请发布您迄今为止尝试过的聚合查询。尝试此{passed_phd:[{'passed_phd':true},{$set:{'passed_phd':“yes”}
在聚合管线中更改字段值?从true更改为yes,从false更改为no@vebey74412:除了true或false
之外,您是否碰巧在passed\u phd
字段中还有其他值?或者是否有任何文档没有passed\u phd
字段?只有true和false存在,每一列都有true或falseE字段值的更改?从真到是,从假到no@vebey74412:除了true或false
,您是否碰巧在passed\u phd
字段中有任何其他值?或者是否存在没有passed\u phd
字段的文档?只有true和false存在,每一列都有true或false
[
{
_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"
}
]