Am 15.07.2012 21:11, schrieb David Eagen:

I have a mongo collection that holds documents which may or may not
contain various fields. I'm trying to determine the correct way to check
for a field. Right now all I have been able to come up with is trying to
deserialize (Bson) or get (Json) the field and catch the exception that
occurs when it isn't valid.

I've tried comparing the type to Json.Type.Undefined but that's not
working for some reason.

What's the right way to check for the existence of a field in a Mongo

According to the BSON spec Undefined is deprecated. For that reason
Type.Null is returned instead, so bson.isNull() or bson.type ==
Bson.Type.Null should work.

Alternatively you can also use bson.myfield.opt!int(-1) != -1 to test
for an integer field "myfield" and get its value at the same time.