需求概述
两个表中都有用户名,在personSubject用户名为code,在person中为userName
当删除userName表中的数据后,要求personSubject表中删除和person表对应的数据
解决方法
persons = db.getCollection("iam.identity.human.subejct.PersonSubject").distinct('code')
db.getCollection("iam.identity.human.individual.Person").remove({userName:{$nin:tokens}})
解析
distinct
- 作用:获取集合中指定字段的不重复值,并以数组的形式返回
- 方法重载一:db.collection_name.distinct(field,query,options)
field -----指定要返回的字段(string)
query-----条件查询(document)
options-----其他的选项(document)
- 方法重载二:db.inventory.distinct(“dept”) //获取dept字段的不重复值
- 方法重载三:db.inventory.distinct(“item.sku”,{dept:”A”}) //满足dept为A数据的item字段的子字段的不重复值
$nin
表示匹配不等于指定值的数据