Using aggregation I am joining two collections of mongodb using golang. The result is seems like below:-
OUTPUT:-
{
"response": {
"code": 1,
"api_status": 1,
"message": "Success",
"total_record": [
{
"_id": 1,
"author_name": "mohit",
"category": 232,
"content": "This is the content",
"date_time": 1524632713,
"excerpt": "This is a short text",
"image": "pic.jpg",
"resultField": [
{
"_id": 6,
"comment": "this is a least comment",
"comment_on": 1524644601,
"email": "puneet@bookingkoala.com",
"name": "puneet",
"post_id": 1,
"reply_to": 1524644601,
"status": 1
},
{
"_id": 7,
"comment": "this is a least comment",
"comment_on": 1524647808,
"email": "puneet@bookingkoala.com",
"name": "puneet",
"post_id": 1,
"reply_to": 1524647808,
"status": 1
}
],
"status": 0,
"tags": "this",
"title": "how to do the code"
},
{
"_id": 2,
"author_name": "mohit",
"category": 232,
"content": "This is the content",
"date_time": 1524632713,
"excerpt": "This is a short text",
"image": "pic.jpg",
"resultField": [
{
"_id": 8,
"comment": "this is a least comment",
"comment_on": 1524648059,
"email": "puneet@bookingkoala.com",
"name": "puneet",
"post_id": 2,
"reply_to": 1524648059,
"status": 1
}
],
"status": 0,
"tags": "this",
"title": "how to do the code"
},
{
"_id": 3,
"author_name": "puneet",
"category": 2,
"content": "this is content",
"date_time": 1524641086,
"excerpt": "this is excerpt",
"image": "pic.jpg",
"resultField": [],
"status": 1,
"tags": "go",
"title": "how to do the code"
}
]
}
}
This output is taken by the below code of golang:-
mongoSession := config.ConnectDb()
collection := mongoSession.DB(config.Database).C(config.BlogCollection)
pipeline := []bson.M{
bson.M{"$match": bson.M{}},
bson.M{"$lookup": bson.M{"from" : "comment", "localField" : "_id", "foreignField": "post_id","as": "resultField" }},
}
fmt.Println(pipeline)
pipe := collection.Pipe(pipeline)
resp := []bson.M{}
err = pipe.All(&resp)
if err != nil {
fmt.Println("Errored: %#v
", err)
}
fmt.Println(resp)
if err != nil {
response = ResponseControllerList{
config.FailureCode,
config.FailureFlag,
config.FailureMsg,
nil,
nil,
}
} else {
response = ResponseControllerList{
config.SuccessFlag,
config.SuccessFlag,
config.SuccessMsg,
nil,
resp,
}
}
Issue is:- I need only count
of the data
not the data
. Means in the output as shown above there is the resultField
in which data is show but I only need the count
value like:- "resultField":[2]
but it is showing the data. How can I will get the count values of the data in output. Thank you in advance.