I develop a REST API
and use Spring Data MongoTemplate
for saving any resources on Mongo
Database.
One of API is :
PUT /devices/{deviceId}
For REST compliant, I should return 200
for an update
or 201
for create
.
When I do a MongoTemplate.save(device)
, how can I know if mongo do insert()
or update()
?
Well, before you save your object into a MongoDB
, check whether its ID
is null
. If null
, you are doing insertion, if not null
, you are doing an update.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.