只是Documents
和List
小号
List<Document> pipeline = Arrays.asList(
new Document("$project",
new Document("data1", 1)
.append("result", new Document(
"$cond", new Document(
"if", new Document("$eq", Arrays.asList("$salary", 250000))
)
.append("then", 30)
.append("else", 20)
))
)
);
序列化为:
[
{ "$project": {
"data1": 1,
"result": {
"$cond": {
"if": { "$eq": [ "$salary", 250000 ] },
"then": 30,
"else": 20
}
}
}}
]
或者交替采用t的$cond
他 “列表” 的形式:
List<Document> pipeline = Arrays.asList(
new Document("$project",
new Document("data1", 1)
.append("result", new Document(
"$cond", Arrays.asList(
new Document("$eq", Arrays.asList("$salary", 250000)),
30,
20
)
))
)
);
序列化为:
[
{ "$project": {
"data1": 1,
"result": {
"$cond": [
{ "$eq": [ "$salary", 250000 ] },
30,
20
}
}
}}
]
两者都是$cond
有效的用法。
还注意到你的问题中的错误,你可能意味着$eq
或你可能意味着$gt
或其他逻辑运算符的条件。 $cond
预计它是“如果”返回一个布尔值。
根据不同的驱动程序,您可以改为Document
而不是BasicDBObject
。