mongo查询过滤条件java实例($and,$or,$eq,$ne)

查询账号下没有org_id ,且对应的company字段也为空,且手机号码是存在的用户


BasicDBObject submitQuery = new BasicDBObject();

submitQuery.put("$and", getQuery());
submitQuery.put("$and", getMobileQuery());


private static List getQuery(){
List list = Lists.newArrayList();

List orgList = Lists.newArrayList();
orgList.add(new BasicDBObject("org_id", new BasicDBObject("$eq", null)));
orgList.add(new BasicDBObject("org_id", new BasicDBObject("$eq", "")));
orgList.add(new BasicDBObject("org_id", new BasicDBObject("$eq", 0L)));

List companyList = Lists.newArrayList();
companyList.add(new BasicDBObject("company", new BasicDBObject("$eq", null)));
companyList.add(new BasicDBObject("company", new BasicDBObject("$eq", "")));


list.add(new BasicDBObject("$or", orgList));
list.add(new BasicDBObject("$or", companyList));
return list;
}


private static List getMobileQuery(){
List phoneList = Lists.newArrayList();
phoneList.add(new BasicDBObject("mobile", new BasicDBObject("$ne", null)));
phoneList.add(new BasicDBObject("mobile", new BasicDBObject("$ne", "")));
return phoneList;
}

你可能感兴趣的:(【MongoDB/Redis】,【Java】)