`

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代码部分实现。主要是红色部分代码。

 

分享到:
评论
1 楼 greatwall3 2016-03-31  
引用
[i][i]第三方的神烦大叔地方地方[/i][/i]

相关推荐

Global site tag (gtag.js) - Google Analytics