I'm trying to find a value within an array, within a document, and return only the array values of the array index where that value was found, along with the outer document data.
Here is an example document in the collection.
{
"_id": {
"$oid": "63452345f14f98447b792327e9c"
},
"TID": "695742332",
"DID": "WW*DHD",
"IndexedSpansUsageCost": [
{
"month": {
"$numberInt": "9"
},
"year": "2022",
"IndexedSpansCost": {
"$numberDouble": "1.11"
},
"IndexedSpansCount": {
"$numberDouble": "418334.0"
}
},
{
"month": {
"$numberInt": "10"
},
"year": "2022",
"IndexedSpansCost": {
"$numberDouble": "1.11"
},
"IndexedSpansCount": {
"$numberDouble": "432516.0"
}
}
]
}
I'm able to find a specific document that has the month of 9, but the output is the entire document, including arrays where the month is 10, 11, etc.
I've tried the following code
multifilter := bson.A{{"IndexedSpansUsageCost.month", bson.D{{"$eq", 9}}}}
cursor, err := collection.Find(context.TODO(), multifilter)
if err != nil {
panic(err)
}
// end find
var resultsTest3 []bson.M
if err = cursor.All(context.TODO(), &resultsTest3); err != nil {
panic(err)
}
for _, resultTest3 := range resultsTest3 {
output, err := json.MarshalIndent(resultTest3, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", output)
}
I've also tried aggregation but haven't gotten this to work properly.
I'm looking to get ONLY the following data.
{
"_id": {
"$oid": "63452345f14f98447b792327e9c"
},
"TID": "695742332",
"DID": "WW*DHD",
"IndexedSpansUsageCost": [
{
"month": {
"$numberInt": "9"
},
"year": "2022",
"IndexedSpansCost": {
"$numberDouble": "1.11"
},
"IndexedSpansCount": {
"$numberDouble": "418334.0"
}
}
]
}
Really having trouble finding out how to get only the array values I'm looking for. Every example I've seen returns the entire document, and all the array values, rather than only the array where the value was found.