Question

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?

Was it helpful?

Solution

I would return an exception for the third case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top