mongodb - Adding an hardcoded property to a json output -
mongodb - Adding an hardcoded property to a json output -
here code in 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}, } }, }} ]) i add together in json output hardcoded properties , values, works
"leaf": {'$literal': true}
but don't know why can't create
"allowdrag": {'$literal':false}, "expanded": {'$literal':false}
is possible $group?
example of output json i've got :
"result" : [ { "_id" : "a", "cpt" : 1, "children" : [ { "text" : "b", "details" : "c", "leaf" : true } ] }] example of output json wish had :
"result" : [ { "_id" : "a", "cpt" : 1, "allowdrag" : false, "expanded" : false, "children" : [ { "text" : "b", "details" : "c", "leaf" : true } ] }]
use $literal operator in $project pipeline homecoming new fields set boolean values of 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 } } ]) tested next collection sample:
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" } ]) the above aggregation gives next results:
/* 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 } json mongodb mongodb-query
Comments
Post a Comment