Mongodb 級聯刪除查詢操作


ObjRelationPojo表一條記錄

 

public class YpObjRelationPojo implements Serializable {

	@Id
	private String id;					// '主鍵id'
	@Field("sourceid")
	private String sourceId;			//對象id
	@Field("targetid")
	private String targetId;			//對象id	
	@Field("caseId")
	private String caseId;				//案件id
	@Transient							//配置透明屬性
	private int source;					// '關系起點   	不存數據庫
	@Transient
	private int target;					// '關系終點	不存數據庫
	@Transient
	private String color;
	@Transient
	private String relation;			// '關系名稱'
	@Field("relation_type")
	@DBRef
	private YpRelationTypePojo relation_type;
	@Field("create_time")
	private Date create_Time;			// '創建時間'

  省略了set,get方法

@DBRef用於關聯對象

{
"_id" : ObjectId("54a24e8a25600d768a4e13ac"),
"sourceid" : "54a20a6dd9d886078991aa98",
"targetid" : "54a209dfd9d886078991aa95",
"caseId" : "100",
"relation_type" : {
"$ref" : "relationtype",
"$id" : ObjectId("54a21b0ad9d886078991aa9e")
}
}

如果需要查找出這條記錄中的relation_type,查看相關聯的對象

我們需要使用點表示法則

Id為relation_type表的記錄id

ObjectId objectId = new ObjectId(Id);

Query query1=Query.query(Criteria.where("relation_type.$id").is(objectId));

List<YpObjRelationPojo> r=mongoTemplate.find(query1, YpObjRelationPojo.class, "YpObjRelationPojo");

找出之后,刪除該字段

Update update=new Update();

           update.unset("relation_type");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM