Luke
Luke

Reputation: 71

How to write $subtract mongo query in go using mgo package?

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 ;(

Upvotes: 3

Views: 819

Answers (1)

Luke
Luke

Reputation: 71

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

Upvotes: 2

Related Questions