Mongodb 如何将$indexOfArray与Go一起使用?

Mongodb 如何将$indexOfArray与Go一起使用?,mongodb,go,aggregation-framework,mgo,Mongodb,Go,Aggregation Framework,Mgo,假设我的mongo客户集合中有以下数据 {customer:"cust1", shops:[ {name:"shop_name1", sales:200}, {name:"shop_name2", sales:300} ]} 在MongoShell中,我可以执行此命令,它返回shops数组中shop_name2的索引,即1 db.customers.aggregate([{"$match":{customer:"cust1"}},{"$project":{"matchedI

假设我的mongo客户集合中有以下数据

{customer:"cust1", 
 shops:[
   {name:"shop_name1", sales:200}, 
   {name:"shop_name2", sales:300}
 ]}
在MongoShell中,我可以执行此命令,它返回shops数组中shop_name2的索引,即1

db.customers.aggregate([{"$match":{customer:"cust1"}},{"$project":{"matchedIndex":{"$indexOfArray":["$shops.name","shop_name2"]}}}])
但在mgo中

err := c.Pipe([]bson.M{{"$match": bson.M{"customer": "cust1"}}, {"$project": bson.M{"matchedIndex": bson.M{"$indexOfArray": []bson.M{{"$shops.name": "shop_name2"}}}}}}).One(&hehehe)
失败,并显示以下消息

无法识别的表达式“$shops.name”

当我查看文档时,我注意到第二个参数是数组。因此,我怀疑我指定的数组是错误的,但我找不到有关如何为mgo设置此数组的任何参考。

的参数只是一个“string”列表,因此
[]string

bson.M{"$indexOfArray": []string{"$shops.name", "shop_name2"}}
或在完整的上下文中:

err := c.Pipe([]bson.M{
 {"$match": bson.M{"customer": "cust1"}},
 {"$project": bson.M{
   "matchedIndex": bson.M{"$indexOfArray": []string{"$shops.name", "shop_name2"}}
 }}
}).One(&hehehe)
到的参数只是一个“string”列表,因此
[]string

bson.M{"$indexOfArray": []string{"$shops.name", "shop_name2"}}
或在完整的上下文中:

err := c.Pipe([]bson.M{
 {"$match": bson.M{"customer": "cust1"}},
 {"$project": bson.M{
   "matchedIndex": bson.M{"$indexOfArray": []string{"$shops.name", "shop_name2"}}
 }}
}).One(&hehehe)
它不是一张“地图”,只是一个常规列表。它不是一张“地图”,只是一个常规列表。