How to use Mongo with Spring Data without ObjectId

后端 未结 4 390
小蘑菇
小蘑菇 2021-01-18 10:23

I have an existing mongo database in which ids (_id) are persisted as plain Strings.. This is sample data in Mongo DB:

{
    \"_id\" : \"528bb0e2e4b0442f1479         


        
4条回答
  •  无人及你
    2021-01-18 10:56

    I think the problem here is a wrong "_id" format and that is why Mongo cannot recognize any object with such id. It should looks like ObjectId("528bb0e2e4b0442f1479f1b4"), not plain String type.

            {
                "_id" : ObjectId("528bb0e2e4b0442f1479f1b4"),
                "schoolId" : "URDLKYLFJXLWJGR193778316742298",
                "surname" : "Lewis",
                "admissionNumber" : "0347",
                "firstName" : "Martins"
            }
    

    So if you have imported .json with existing data to mongo you need to change id:

    "_id": {$oid:"528bb0e2e4b0442f1479f1b4"},
    

    And it should be converted to correct format.

    Hope it can help someone.

提交回复
热议问题