将硬编码 属性 添加到 json 输出
Adding an hardcoded property to a json output
这是我在 mongodb 中的代码:.
db.mydb.aggregate([
{ "$group": {
"_id": {
"A": "$A",
"B": "$B",
"C": "$C"
},
}},
{ "$group": {
"cpt": { '$sum': 1 } ,
"_id": "$_id.A",
"allowDrag": {'$literal':false},
"expanded": {'$literal':false},
"children": {
"$push": {
"text": "$_id.B",
"details": "$_id.C",
"leaf": {'$literal': true},
}
},
}}
])
我想在我的 json 输出中添加一些硬编码的属性和值,它适用于
"leaf": {'$literal': true}
但我不知道为什么我不能用
"allowDrag": {'$literal':false},
"expanded": {'$literal':false}
$group 可以吗?
输出示例 json 我有:
"result" : [
{
"_id" : "A",
"cpt" : 1,
"children" : [
{
"text" : "B",
"details" : "C",
"leaf" : true
}
]
}]
输出示例 json 我希望我有 :
"result" : [
{
"_id" : "A",
"cpt" : 1,
"allowDrag" : false,
"expanded" : false,
"children" : [
{
"text" : "B",
"details" : "C",
"leaf" : true
}
]
}]
使用 $literal
operator in the $project
管道 return 将新字段设置为布尔值 false:
db.mydb.aggregate([
{
"$group": {
"_id": {
"A": "$A",
"B": "$B",
"C": "$C"
}
}
},
{
"$group": {
"cpt": { '$sum': 1 } ,
"_id": "$_id.A",
"children": {
"$push": {
"text": "$_id.B",
"details": "$_id.C",
"leaf": {'$literal': true}
}
}
}
},
{
"$project": {
"allowDrag": {'$literal':false},
"expanded": {'$literal':false},
"cpt": 1,
"children": 1
}
}
])
使用以下集合样本进行测试:
db.mydb.insert([
{
"A": "test1",
"B": "test2",
"C": "test3"
},
{
"A": "test1",
"B": "test2",
"C": "test2"
},
{
"A": "test2",
"B": "test2",
"C": "test3"
},
{
"A": "test2",
"B": "test2",
"C": "test3"
}
])
以上聚合给出了以下结果:
/* 0 */
{
"result" : [
{
"_id" : "test1",
"cpt" : 2,
"children" : [
{
"text" : "test2",
"details" : "test2",
"leaf" : true
},
{
"text" : "test2",
"details" : "test3",
"leaf" : true
}
],
"allowDrag" : false,
"expanded" : false
},
{
"_id" : "test2",
"cpt" : 1,
"children" : [
{
"text" : "test2",
"details" : "test3",
"leaf" : true
}
],
"allowDrag" : false,
"expanded" : false
}
],
"ok" : 1
}
这是我在 mongodb 中的代码:.
db.mydb.aggregate([
{ "$group": {
"_id": {
"A": "$A",
"B": "$B",
"C": "$C"
},
}},
{ "$group": {
"cpt": { '$sum': 1 } ,
"_id": "$_id.A",
"allowDrag": {'$literal':false},
"expanded": {'$literal':false},
"children": {
"$push": {
"text": "$_id.B",
"details": "$_id.C",
"leaf": {'$literal': true},
}
},
}}
])
我想在我的 json 输出中添加一些硬编码的属性和值,它适用于
"leaf": {'$literal': true}
但我不知道为什么我不能用
"allowDrag": {'$literal':false}, "expanded": {'$literal':false}
$group 可以吗?
输出示例 json 我有:
"result" : [
{
"_id" : "A",
"cpt" : 1,
"children" : [
{
"text" : "B",
"details" : "C",
"leaf" : true
}
]
}]
输出示例 json 我希望我有 :
"result" : [
{
"_id" : "A",
"cpt" : 1,
"allowDrag" : false,
"expanded" : false,
"children" : [
{
"text" : "B",
"details" : "C",
"leaf" : true
}
]
}]
使用 $literal
operator in the $project
管道 return 将新字段设置为布尔值 false:
db.mydb.aggregate([
{
"$group": {
"_id": {
"A": "$A",
"B": "$B",
"C": "$C"
}
}
},
{
"$group": {
"cpt": { '$sum': 1 } ,
"_id": "$_id.A",
"children": {
"$push": {
"text": "$_id.B",
"details": "$_id.C",
"leaf": {'$literal': true}
}
}
}
},
{
"$project": {
"allowDrag": {'$literal':false},
"expanded": {'$literal':false},
"cpt": 1,
"children": 1
}
}
])
使用以下集合样本进行测试:
db.mydb.insert([
{
"A": "test1",
"B": "test2",
"C": "test3"
},
{
"A": "test1",
"B": "test2",
"C": "test2"
},
{
"A": "test2",
"B": "test2",
"C": "test3"
},
{
"A": "test2",
"B": "test2",
"C": "test3"
}
])
以上聚合给出了以下结果:
/* 0 */
{
"result" : [
{
"_id" : "test1",
"cpt" : 2,
"children" : [
{
"text" : "test2",
"details" : "test2",
"leaf" : true
},
{
"text" : "test2",
"details" : "test3",
"leaf" : true
}
],
"allowDrag" : false,
"expanded" : false
},
{
"_id" : "test2",
"cpt" : 1,
"children" : [
{
"text" : "test2",
"details" : "test3",
"leaf" : true
}
],
"allowDrag" : false,
"expanded" : false
}
],
"ok" : 1
}