mgo time.Time or boolean check

只谈情不闲聊 提交于 2019-12-08 08:00:29

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.

Query if the field is false:

iter := collection.Find(bson.M{"field": false}).Iter()

Query if the field is available, with the $exists operator:

iter := collection.Find(bson.M{"field": bson.M{"$exists": true}}).Iter()

Query if the field is a date, using the $type operator:

iter := collection.Find(bson.M{"field": bson.M{"$type": 9}}).Iter()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!