【发布时间】:2020-02-27 10:52:34
【问题描述】:
这是我的实体
@Document(collection = "deviceConfig")
@Data
@Builder
public class DbDeviceConfig {
private ObjectId _id;
// @Field("id")
// @Id
// private String id;
private String deviceId;
private String enterpriseId;
private Integer state;
private String cfgKey;
@JsonSerialize
private String cfgValue;
private Long cfgDate;
private Long reportDate;
private Integer reportCode;
private String reportMsg;
}
如您所见,我注意到@Id
因为在我的情况下,每个设备将只有一个特定 cfgKey 的 cfgValue
因此 enterpriseId 和 deviceId 以及 cfgKey 将保持其唯一性
我写了 mongotemplate 更新 sql
Query updateQuery = new Query(Criteria.where("enterpriseId").is(enterpriseId)
.and("cfgKey").is(batchAddDeviceConfig.getCfgKey())
.and("deviceId").is(deviceId));
Update update = new Update();
update.set("cfgDate", System.currentTimeMillis());
update.set("cfgValue", batchAddDeviceConfig.getCfgValue());
update.set("state", batchAddDeviceConfig.getState());
dataMongoTemplate.upsert(updateQuery, update, DbDeviceConfig.class, "deviceConfig");
但我不知道如何编写对应的spring jpa代码
或者我应该添加一个企业 ID?
我真的很困惑,因为 mongodb 已经创建了一个 ObjectId
我也在寻找其他人的解决方案
看来我可以用这种方式
@Modifying
@Query("UPDATE Space c SET c.owner = :name WHERE c.id = :id")
Integer setNameForId(@Param("name") String name, @Param("id")
但这会像很多参数
请帮我QAQ
【问题讨论】:
-
这里是一些关于使用 MongoDB 和 Spring Data API 的参考信息:Spring Data MongoDB - Reference Documentation。有几种方法可以执行 CRUD 操作 - 使用
MongoRepository和MongoTemplateAPI。 -
@prasad_ 谢谢,我会多注意这个文件的:)
-
您使用的是 Spring Data JPA 还是 Spring Data MongoDb。这是两个不同的东西。
标签: java mongodb spring-data-jpa mongodb-query spring-data-mongodb