mongodb之DBRef的java应用
List<DBObject> listObj = new ArrayList<DBObject>();
DBCollection coll_A = CrawlerMongoDBManager.getCollection(Constant.DB_CONN_PRODUCT);
DBCollection coll_B = MatchMongodbManger.getCollection(
Constant.MONGODB_ITEM);
for (ItemVO itemVO : listvos) {
List<DBRef> listRef = new ArrayList<DBRef>();
List<DBObject> websiteList = new ArrayList<DBObject>();
String createTime = itemVO.getCreateTime();
String websiteId = itemVO.getWebsiteId();
String catalogId = itemVO.getCatalogId();
DBObject item = new BasicDBObject();
item.put("catalogId", catalogId);
item.put("websiteId", websiteId);
item.put("itemSku", itemVO.getItemId());
BasicDBObject keys = new BasicDBObject();
keys.put("_id", 1);
DBObject dbObject = coll.findOne(item,keys);
String pId = dbObject.get("_id") != null ? dbObject.get("_id")
.toString() : "";
DBRef refB = new DBRef(new BMongodbManger().getDB(),
Constant.MONGODB_BRAND, new ObjectId(itemVO
.getBrandId()));
DBRef refP = new DBRef(new AMongoDBManager().getDB(),
Constant.DB_CONN_PRODUCT, new ObjectId(pId));
listRef.add(refP);
DBObject websiteObj = new BasicDBObject();
websiteObj.put("websiteId", websiteId);
websiteList.add(websiteObj);
DBObject subObj = new BasicDBObject();
subObj.put("brand", refB);
subObj.put("catalogId", catalogId);
subObj.put("createTime", createTime);
subObj.put("productList", listRef);
subObj.put("websiteList", websiteList);
listObj.add(subObj);
}
coll_match.insert(listObj);
上面为mongodb多表关联的java代码部分实现。主要是红色部分代码。