0

I'm writing a Java program using MongoDB. I have one class with a function that could have one of three outcomes:

  1. Look for suitable object for the function's input in MongoDB collection, find it, and return its ObjectId.
  2. As above, but don't find it; create it, and return its ObjectId.
  3. Input turns out not to be suitable to begin with; return some kind of flag value to the caller that indicates to just ignore that input.

The first two cases are easy. I'm not sure what kind of value I can return for the third. A dummy ObjectId like 000000000000 is rejected as invalid.

The solutions I can think of are to return the ObjectIds as strings and turn them back into ObjectIds later (then I can return any flag-type string I want for the third case), or create a dummy database item that actually means no result (but where do I keep track of its ObjectId so I don't have to keep looking it up?). Any better ideas of how to approach this?

2
  • As @milan says. Or why do you want to save input that is not suitable? Dec 15, 2011 at 22:29
  • At an earlier stage in the project I was doing something with it... and I do want to mark it as not suitable so if it's encountered again, I don't bother with it. But, as you both point out, an exception makes more sense the way it's set up now and I can handle it there.
    – umbraphile
    Mar 5, 2012 at 21:29

1 Answer 1

1

I would return an exception for the third case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.