Java Spring数据、MongoTemplate save()和REST
我开发了一个Java Spring数据、MongoTemplate save()和REST,java,spring,api,rest,mongotemplate,Java,Spring,Api,Rest,Mongotemplate,我开发了一个restapi,并使用Spring数据MongoTemplate保存Mongo数据库上的任何资源 其中一个API是: PUT/devices/{deviceId} 对于REST兼容,我应该返回200进行update或201进行create 当我执行MongoTemplate.save(设备)时,我如何知道mongo是否执行insert()或update()?在将对象保存到MongoDB之前,请检查其ID是否为null。如果null,您正在执行插入操作,如果不是null,您正在执行更新
restapi
,并使用Spring数据MongoTemplate
保存Mongo
数据库上的任何资源
其中一个API是:
PUT/devices/{deviceId}
对于REST兼容,我应该返回200
进行update
或201
进行create
当我执行
MongoTemplate.save(设备)
时,我如何知道mongo是否执行insert()
或update()
?在将对象保存到MongoDB
之前,请检查其ID
是否为null
。如果null
,您正在执行插入操作,如果不是null
,您正在执行更新操作。您好,在我的情况下,我的对象ID永远不会为null,因为它是一个函数ID,并且通过转换器映射到“\u ID”。其他解决方案?