spring-projects / spring-projects/spring-data-mongodb

mongoTemplate sends strings as objectIds

Open
#4,466 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

for: team-attention status: feedback-provided status: waiting-for-triage theme: logging
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

mongoTemplate seems to automatically convert strings to ObjectIds, which can lead to undesired behavior

I'm using mongo 6.x and spring-data-mongo 4.0.8

See the following test and its log output:

    @Test
    void mongoObjectIdConversion() {
        ObjectId objectId = ObjectId.get();
        mongoTemplate.updateFirst(Query.query(where("_id").is(objectId)), new Update().set("foo", "bar"), "testcol");
        mongoTemplate.updateFirst(Query.query(where("_id").is(objectId.toHexString())), new Update().set("foo", "bar"), "testcol");
        mongoTemplate.updateFirst(Query.query(where("_id").is(objectId.toHexString() + "x")), new Update().set("foo", "bar"), "testcol");
    }
2023-08-07T14:15:12.282+02:00 DEBUG 15565 --- [           main] o.s.data.mongodb.core.MongoTemplate      : Calling update using query: { "_id" : { "$oid" : "64d0e050450ebf73e850fafc"}} and update: { "$set" : { "foo" : "bar"}} in collection: testcol
2023-08-07T14:15:12.287+02:00 DEBUG 15565 --- [           main] o.s.data.mongodb.core.MongoTemplate      : Calling update using query: { "_id" : { "$oid" : "64d0e050450ebf73e850fafc"}} and update: { "$set" : { "foo" : "bar"}} in collection: testcol
2023-08-07T14:15:12.289+02:00 DEBUG 15565 --- [           main] o.s.data.mongodb.core.MongoTemplate      : Calling update using query: { "_id" : "64d0e050450ebf73e850fafcx"} and update: { "$set" : { "foo" : "bar"}} in collection: testcol

When the records are effectively using "String" as datatype for _id, then the second query will not update the records. I would expect the update to keep the original data type.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied mongoObjectIdConversion test with MongoDB 6.x and spring-data-mongo 4.0.8, then trace MongoTemplate's query conversion for _id values. Compare the logged queries for ObjectId, a valid hexadecimal string, and a non-hexadecimal string. Done means a String _id remains a String rather than being converted to an ObjectId.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.