Object deriveDocumentId(BSONObject selector) {
   Object value = selector.get(idField);
   if (value != null) {
     if (!Utils.containsQueryExpression(value)) {
       return value;
     } else {
       return deriveIdFromExpression(value);
     }
   }
   return new ObjectId();
 }
  /** convert selector used in an upsert statement into a document */
  BSONObject convertSelectorToDocument(BSONObject selector) throws MongoServerException {
    BSONObject document = new BasicBSONObject();
    for (String key : selector.keySet()) {
      if (key.startsWith("$")) continue;

      Object value = selector.get(key);
      if (!Utils.containsQueryExpression(value)) {
        changeSubdocumentValue(document, key, value, (AtomicReference<Integer>) null);
      }
    }
    return document;
  }