【发布时间】:2021-09-19 11:05:04
【问题描述】:
我有一个包含人员及其信息(姓名、性别等...)的数据库 示例:
{
"id": 31,
"balance": "$1,137.95",
"age": 24,
"eyeColor": "blue",
"name": "Burris Newton",
"gender": "male",
"company": "XOGGLE",
"email": "burrisnewton@xoggle.com",
}
我想使用 $cond 将数据库中的性别替换为“H”和“F”替换“男性”
我已经写了这个,但它不起作用
db.contacts.aggregate([
{
$project: {
_id: 0,
age: 1,
name: 1,
gender: {
$cond: {
if: {
"gender": {$eq: ["$gender", "male"]},
then: "H",
else: "F"
}
}
}
}
}
])
【问题讨论】:
-
你可以从$cond document找到正确的语法,更正这个
if: { $eq: ["$gender", "male"] } -
@turivishal 这就是使用的东西,这就是为什么我不明白为什么它不起作用
-
再次验证两者是否不同 这是不正确的 >
{ if: { "gender":{ $eq: ["$gender", "male"]}这是正确的 >if: { $eq: ["$gender", "male"] }
标签: mongodb