MorphiaOrg / morphia

MongoDB object-document mapper in Java based on https://github.com/mongodb/mongo-java-driver
Apache License 2.0
1.65k stars 456 forks source link

Filters.eq Doesn't Work With String Objects #2853

Closed gursahibsahni closed 8 months ago

gursahibsahni commented 8 months ago

Describe the bug Using Filters.eq for String based fields doesn't work. Example:

// DOES NOT work datastore.find(CustomerDAO.class).filter(Filters.eq("activePhone", activePhone)).iterator().tryNext();

// DOES work datastore.find(CustomerDAO.class).filter(Filters.eq("_id", new ObjectId(id))).iterator().tryNext();

// mongo client DOES work datastore.getDatabase().getCollection("customer").find(eq("activePhone", activePhone));

Expected behavior Morphia should return data based on the activePhone data image

Please complete the following information:

evanchooly commented 8 months ago

Any particular reason you're querying against CustomerDAO and not Customer?

gursahibsahni commented 8 months ago

Any particular reason you're querying against CustomerDAO and not Customer?

Doesn't matter -- CustomerDAO's collection name is customer. They both point to the same thing. Updated the question with clarity and better formatting.

image
gursahibsahni commented 8 months ago

FWIW - I am using the following codec registry in my datastore config

final var pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));

evanchooly commented 8 months ago

If you could put together a reproducer I'd be happy to take a look and see. This case is pretty well tested and years old so there must be something else in play I'm not seeing here.

gursahibsahni commented 8 months ago

@evanchooly I have added you to the project I am working on. I think that should help you to see the problem. Please let me know if by anyway I can help you more to look into it. You would need mongo on your local to run it and once you build. You would need to add a customer

POST http://localhost:8080/customers

{ "isNewCustomer": true, "name": "Gursahib", "address": [ { "number": "string", "sector": "string", "city": "New Delhi", "country": "India", "pincode": "string", "isActive": true } ], "activePhone": "9811723480", "phone": [ "0909090909" ], "notes": "customer notes", "customerTag": { "name": "big customer", "meaning": "some meaning" }, "customerOrderTag": { "name": "order tag", "meaning": "some meaning" } }

Then GET http://localhost:8080/customers?pn=9811723480

You would be able to see no results and be able to debug.

Thank you so much

gursahibsahni commented 8 months ago

@evanchooly I used to reproducer repo and figured out the problem. I still don't know what was the root issue. But here is the thing - If you see the screenshot of the above mongo compass - there is no type discriminator to tell mongo how to deserialize. I think I was using an old version of Mongo and after the change in the version, the change wasn't percolated in IntelliJ (I hate when it does that). Now, when I have changed the mongo version, I see the _t field has a value and can retrieve the value.

image

We can close the issue.

evanchooly commented 8 months ago

If you're working with data from older versions, look at the legacy() method on MapperOptions to recreate the older default settings.