Mongodb’s upsert method insert data id is null?

  mongodb, question

Spring integrates MongoDB, and wants to realize the functions of adding and modifying in one method. After using mongoTemplate.upsert, it is found that the id value of inserting new data is null, but there is an id for inserting with insert. May I ask why? How to solve it? I’m not very familiar with MongoDB just now.

I don’t know Spring very well either … After checking the documents,upsertThe method returnscom.mongodb.client.result.UpdateResult. There aregetUpsertedId

If the replace resulted in an inserted document, gets the _id of the inserted document, otherwise null.

This should be the ID you want.