mgo time.Time or boolean check

萝らか妹 提交于 2019-12-23 02:17:38

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/18721801/mgo-time-time-or-boolean-check

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!