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

有只小跳蛙
TA貢獻1824條經(jīng)驗 獲得超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.
這應(yīng)該就是你要的ID
- 1 回答
- 0 關(guān)注
- 1675 瀏覽
添加回答
舉報
0/150
提交
取消