如何使用mgo包在Go语言中编写$subtract的Mongo查询?

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

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

huangapple
  • 本文由 发表于 2015年3月6日 07:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/28889698.html
匿名

发表评论

匿名网友

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

确定