如何用混合类型表示一个数组 [英] How to represent an array with mixed types
问题描述
我正在用MongoDB中的 $ substr
命令构建一个聚合管道查询,但我不知道如何使用mgo驱动程序在Go中表示它需要的数组,因为它包含不同类型的值(string,int)。
以下是javascript中的查询: 这个试图做的是得到 在Go中,我得到了: 但 您可以使用 语法有点脏,你可以很容易地定义一个本地类型,使它看起来更好: I am constructing an aggregation pipeline query with the Here is the query in javascript: What this is trying to do is get the substring of In Go i got: but You can represent these values using a slice of type If you find the syntax a little dirty, you can easily define a local type to make it look nicer:
这篇关于如何用混合类型表示一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<$ p $ [{$ group:{_id:{dt:{$ substr:[$ dt,0,6]}}}}]
dt
的子字符串
<$ p $ b。{$ group:bson.M {_id:bson.M {dt:bson.M {$ substr:[$ dt ,0,6]}}}}}
[$ dt ,0,6]
不是正确的表示形式,我尝试的所有内容似乎都失败了。
[] interface {}
类型的片段来表示这些值:
l:= [] interface {} {$ dt,0,6}
type list [] interface {}
l:= list {$ dt,0,6}
$substr
command from MongoDB but I don't know how to represent the array it requires in Go with the mgo driver because it contains different types of values (string, int).[ {$group: {"_id": {"dt": {"$substr": ["$dt",0,6]}}}} ]
dt
(from the previous stage of aggregation) with starting index 0 and ending index 6.[]bson.M{"$group": bson.M{"_id": bson.M{"dt": bson.M{"$substr": ["$dt",0,6]}}}}}
["$dt",0,6]
is not a correct representation and everything I tried seems to fail.[]interface{}
: l := []interface{}{"$dt", 0, 6}
type list []interface{}
l := list{"$dt", 0, 6}