在MongoDB中查找文档并返回只包含特定值的数组元素。

huangapple go评论85阅读模式
英文:

Find a document and return only array elements with a specific value in MongoDB

问题

我正在尝试在文档中的数组中查找一个值,并返回该值所在数组索引的数组值,以及外部文档数据。

以下是集合中的示例文档:

{
    "_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"
        }
      }
    ]
  }

我能够找到一个具有月份为9的特定文档,但输出是整个文档,包括月份为10、11等的数组。

我尝试了以下代码:

multifilter := bson.A{{"IndexedSpansUsageCost.month", bson.D{{"$eq", 9}}}}
cursor, err := collection.Find(context.TODO(), multifilter)
if err != nil {
    panic(err)
}
// 结束查找

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)
}

我还尝试了聚合,但还没有成功。

我希望仅获取以下数据:

{
    "_id": {
      "$oid": "63452345f14f98447b792327e9c"
    },
    "TID": "695742332",
    "DID": "WW*DHD",
    "IndexedSpansUsageCost": [
      {
        "month": {
          "$numberInt": "9"
        },
        "year": "2022",
        "IndexedSpansCost": {
          "$numberDouble": "1.11"
        },
        "IndexedSpansCount": {
          "$numberDouble": "418334.0"
        }
      }
    ]
  }

我真的很难找到只获取我要查找的数组值的方法。我看到的每个示例都返回整个文档和所有数组值,而不仅仅是找到值的数组。

英文:

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.

答案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)
}

英文:
	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)
}

huangapple
  • 本文由 发表于 2022年11月18日 02:55:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/74480708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定