mongodb-javaspring-mongodb

Java driver - BSON filter check if record is deleted?


I am trying to get valid records from mongodb using Spring - mongodb - java driver
my question is around the creation of the Bson filter

I am looking for matching records that match some filter (printer_Type and manufacture_Id ) and the records aren't deleted (isDeleted=true)
my issue is that isDeleted can exist or not on the record.
so including that exists clause made me confused.

I wasn't able to do it in mongo without java https://mongoplayground.net/p/U0N6FJLUWTk

Bson filter = and(
                and(
                   eq("printer_Type",ConfigurationTypes.PRINTER_TYPES)
                   ,
                   eq("manufacture_Id",manufactureId)
                ,
               or(
                       exists("isDeleted"),
                       eq("isDeleted",Boolean.FALSE)
                  ))
                );

so for example for this list

    [
{"manufacture_Id":"XEROX",  "printer_Type":"officeJet", "printerTypeName":"Office Jet", isDeleted:true },
{"manufacture_Id":"XEROX",  "printer_Type":"homeJet","printerTypeName":"Home Jet",     },
{"manufacture_Id":"HP",      "printer_Type":"homeJet","printerTypeName":"Home Jet",     },
{"manufacture_Id":"XEROX",  "printer_Type":"homeJet1","printerTypeName":"Home Jet 1",   isDeleted:false  },
{"manufacture_Id":"XEROX",  "printer_Type":"homeJet2","printerTypeName":"Home Jet 2",   isDeleted:true   },
{"manufacture_Id":"XEROX",  "printer_Type":"homeJet3","printerTypeName":"Home Jet 3",   isDeleted:true   },
{"manufacture_Id":"XEROX",  "printer_Type":"homeJet4","printerTypeName":"Home Jet 4"   }
]

So for manufacure id XEROX (and any printer type) I should get back:

Home jet
Home Jet 1
Home Jet 4

full code:

    Bson projectionFields = Projections.fields(
            Projections.include("printerTypeName"),
            Projections.excludeId());
    
    Bson filter = and(
                    and(
                       eq("printer_Type",ConfigurationTypes.PRINTER_TYPES)
                       ,
                       eq("manufacture_Id",manufactureId)
                    ,
                   or(
                           exists("isDeleted"),
                           eq("isDeleted",Boolean.FALSE)
                      ))
                    );
    
    Document doc = collection.find(filter)
            .projection(projectionFields)
            .sort(Sorts.descending("printerType"))
            .first();
            
    if (doc == null) {
        System.out.println("No results found for printer Type Name");
        return null;
    } else {
        System.out.println(doc.toJson());
        return doc.getString("printerTypeName");
    }

Solution

  • So for manufacure id XEROX (and any printer type) I should get back:

    Home jet Home Jet 1 Home Jet 4

    For any printer_Type the required query would look like this

    db.collection.find({
      "manufacture_Id": "XEROX",
      "isDeleted": {
        $ne: true
      }
    })
    

    You can try it here

    Bson filter what are you looking for based on your code snippet:

    import com.mongodb.client.model.Filters;
    import org.bson.conversions.Bson;
    
    Bson filter = Filters.and(Filters.eq("manufacture_Id", manufactureId), Filters.eq("printer_Type", ConfigurationTypes.PRINTER_TYPES), Filters.ne("isDeleted", true));
    

    ...