spring集成MongoDB,想在一個方法里實現增加和修改的功能,用mongoTemplate.upsert后發現插入新數據的id值是null,但是用insert插入是有id的,請問這是為什么?如何解決?剛接觸MongoDB還不是很熟
1 回答

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
對Spring也不是特別熟……查了一下文檔,upsert
方法返回的是com.mongodb.client.result.UpdateResult
。這個對象中有getUpsertedId
:
If the replace resulted in an inserted document, gets the _id of the inserted document, otherwise null.
這應該就是你要的ID
- 1 回答
- 0 關注
- 1665 瀏覽
添加回答
舉報
0/150
提交
取消