zoukankan      html  css  js  c++  java
  • 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");

  • 相关阅读:
    11.枚举类.md
    10. Lambda表达式.md
    9.内部类
    8.抽象类、接口和多态.md
    7.final关键字.md
    jQuery学习笔记(5)-事件与事件对象
    Log4Net学习笔记(1)-完整的例子
    SqlServer知识点-操作xml
    NHibernate常见错误汇总(0)-持续更新中
    jQuery学习笔记(4)-设置元素的属性和样式
  • 原文地址:https://www.cnblogs.com/bornteam/p/4193763.html
Copyright © 2011-2022 走看看