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