mgo time.Time或布尔检查 [英] mgo time.Time or boolean check
问题描述
我有一个包含日期字段的mongo文档,该日期字段也可以是假的(或未定义的),并且我似乎无法找到如何检查该字段是否可用或者是否为OR或日期(时间。时间)in golang / mgo:S
I have a mongo document which contains a date field which can also be false (or not defined), and I can't seem to find how to check if the field is available OR is false OR is a date (time.Time) in golang/mgo :S
推荐答案
如果您有 time.Time
字段,并且想要知道它是否设置了有效日期,那么可以查询它的 IsZero()
方法。否则,如果您试图查询数据库中的这种文档,您可以执行以下任一操作。
If you have a time.Time
field, and want to know whether it was properly set with a valid date, you can query its IsZero()
method. Otherwise, if you're trying to query the database for such a document, you can do one of the following.
查询字段是否为false:
Query if the field is false:
iter := collection.Find(bson.M{"field": false}).Iter()
使用 $ exists operator :
Query if the field is available, with the $exists operator:
iter := collection.Find(bson.M{"field": bson.M{"$exists": true}}).Iter()
使用 $ type运算符查询字段是否为日期:
Query if the field is a date, using the $type operator:
iter := collection.Find(bson.M{"field": bson.M{"$type": 9}}).Iter()
这篇关于mgo time.Time或布尔检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!