1

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.

2
  • 1
    doe's this answer your question? Commented Nov 17, 2022 at 19:50
  • Yes it does. I'm posting another answer that works, I'm not sure what's best for DB performance. Commented Nov 17, 2022 at 20:29

1 Answer 1

1
    matchStage  := bson.D{{"$match", bson.D{{"IndexedSpansUsageCost.month", "9"}}}}
    unwind  := bson.D{{"$unwind", "$IndexedSpansUsageCost"}}
    matchAgain  := bson.D{{"$match", bson.D{{"IndexedSpansUsageCost.month", "9"}}}}


cursor, err := collection.Aggregate(context.TODO(), mongo.Pipeline{matchStage, unwind, matchAgain})
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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.