mongodb for java使用中的小问题

1.批量更新老是忘掉后面的true

DBObject d = pCursor.next();
String id = d.get("id").toString();
Pattern p = Pattern.compile("^" + id,Pattern.CASE_INSENSITIVE);
BasicDBObject where = new BasicDBObject("id", p);
BasicDBObject field = new BasicDBObject("parentId", id);
BasicDBObject fields = new BasicDBObject("$set", field);
city.update(where, fields,false,true);

2.去掉默认的_id

比较纠结,在客户端可以去掉查询结果的_id,但在java里怎么也去不掉

原来跟_id所在的位置有关系,如下:_id必须是field中的第一个key,如果_id跟code,name任意掉换下位置就过滤不掉了..

BasicDBObject where = new BasicDBObject("parentId", cityId);

BasicDBObject field = new BasicDBObject("_id", 0);

field.put("code", 1);

field.put("name", 1);


DBCursor cursor = weatherCode_coll.find(where, field);

你可能感兴趣的:(java,mongodb)