hi,
I am using mongo db data source my collection structure is like this:

{
  "course name": [
    {
      "total": 10000,
      "_id": "Master of Business Administration"
    }],
  "id" : 1234,
  "type" : "university"
}

What i need to do is i need to fetch only total value in the array course name and id and type . Can anyone help me in this

You can do this using the fields key. Assume your collection is called foo, the query looks like this:

{
	"collection": "foo",
	"fields": {
		"_id": 1,
		"type": 2,
		"course_name.0": 3
	}
}
1 Like

hi,
I have tried this but in my case it is not working
I am sharing my data structure i am using MongoDB what i need to do is fetch the total value in course name. Course name is my column and my fields are in array format it stores the multiple value like this -:

  1. [{“total”:10000,“_id”:“Master of Business Administration”}]
  2. [{“total”:446,“_id”:“Master of Business Administration”},{“total”:10850,“_id”:“Master of Computer Application”}]
    I need to fetch the total value in the array and sum up the value to find total student registered . how do i calculate it
    image

Ah, in that case you’ll need to $unwind the array into a separate set of documents and aggregate that way.

hi,
Thanks for replying. i have wrote the query but i am stuck somwhere. i am not able to add the value in the mutlple array . i want my output like this -:
[{“total”:10000}]
[{“total”:11296}]

below is my query -:

{
    "collection": "nad_stats",
    "$unwind": "$COURSE_NAME",
    "fields": {
        "COURSE_NAME": 1
    },
    "aggregate": [
        {
            "$project": {
                "COURSE_NAME.total": 1,
                "total": {
                    "$sum": "$COURSE_NAME.total"
                }
            
            }
        }
    ]
}

I have attached the output of the query -:

image

It’s not clear to me what the issue is here. Your last screenshot seems to have output the format you need :confused:

Have you queried MongoDB before? From a CLI? Or is this your first-time?

Nope the screen shot i mention above is not the output this is my column where i need to find the total value of the coursename . I need to to sum up all the total value . yes i am new to mongo db