英文:
How to write $subtract mongo query in go using mgo package?
问题
如何使用mgo包在Go语言中编写以下查询:
a:{$subtract:[variable,'$created']}
我尝试了以下代码:
date := time.Now()
bson.M{
"a": bson.M{
"$subtract": bson.M{date, "$created"}
}
}
但是bson.M是一个映射类型,并且要求提供键值;(
英文:
How to write following query in go using mgo package:
a:{$subtract:[variable,'$created']}
I tried
date := time.Now()
bson.M{
"a":bson.M{
"$subtract":bson.M{date,"$created"}
}
}
but bson.M is a map and asks me for keys ;(
答案1
得分: 2
问题是数组中包含time.Time
结构和string
,所以它是混合类型的数组...但我想我找到了答案:https://stackoverflow.com/questions/19055037/how-to-represent-an-array-with-mixed-types
type list []interface{}
date := time.Now()
sub := list{date, "$created"}
bson.M{
"a": bson.M{
"$subtract": sub
}
}
请注意,这只是代码的翻译部分,不包括问题的回答。
英文:
the problem is that array would contain time.Time
structure and string
, so it is mixed type array... but i think i found the answer: https://stackoverflow.com/questions/19055037/how-to-represent-an-array-with-mixed-types
type list []interface{}
date := time.Now()
sub := list{date, "$created"}
bson.M{
"a":bson.M{
"$subtract":sub
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论