zl程序教程

您现在的位置是:首页 >  其他

当前栏目

NoSQLBooster for MongoDB 中跨库关联查询

2023-02-26 09:50:49 时间

使用 MongoDB 是我们常常会遇到一些特殊的需求需要跨库关联查询,比如订单明细缺商品重量需要补商品重量,而商品重量数据又在商品库中,这事就需要跨库关联操作,示例代码如下:

// 使用 order 库,注意语句后面不要加分号
use order

(福利推荐:阿里云、腾讯云、华为云服务器最新限时优惠活动,云服务器1核2G仅88元/年、2核4G仅698元/3年,点击这里立即抢购>>>

var count = ;
db.order_detail.find({“store_code”:”110″}).forEach(function(_order){

var item = db.getSiblingDB("goods").item.findOne({"barcode":_order.barcode}); if(item){     db.order_detail.update({_id:_order._id},{$set:{"weight":item.weight}},false,true);     count++; }else{     print("商品不存在, 条码:" + _order.barcode); }

});
print(“更新条数:” + count);
注意:跨库查询时必须使用 admin 库来授权连接。

上面示例的代码,数量不多时还能勉强凑合着使用。当数据量达到上万条数据时就显示非常非常慢。因为更新一条QQ账号数据需要单条 findOne 在单条 update。因此得优化,将单条查询改批量查询(缓存查询结果),示例代码如下:

use order

var count = ;
var items = {};
db.getSiblingDB(“item”).goods.find({“store_code”:”110″}).forEach(function(_item){

items[_item.barcode] = _item;

});
db.order_detail.find({“store_code”:”110″}).forEach(function(_order){

var item = items[_order.barcode]; if(item){     db.order_detail.update({_id:_order._id},{$set:{"weight":item.weight}},false,true);     count++; }else{     print("商品不存在, 条码:" + _order.barcode); }

});
print(“更新条数:” + count);

进过将单条查询改成批量查询后执行效率确实提升不少,但是还是觉得慢,还得继续优化,将单条更新改成批量更新,示例代码如下:

use order

var count = ;
var items = {};
db.getSiblingDB(“item”).goods({“store_code”:”110″}).forEach(function(_item){

items[_item.barcode] = _item;

});
var ops = [];
db.order_detail.find({“store_code”:”110″}).forEach(function(_order){

var item = items[_order.barcode]; if(item){     var f = {_id:_order._id};     var upd = {$set:{"weight":item.weight}};     ops.push({"updateMany":{"filter":f, "update":upd, "upsert":false}});     count++; }else{     print("商品不存在, 条码:" + _order.barcode); }  if(count >  && count % 1000 == ){     // 批量更新, ordered:false 无序操作     db.order_detail.bulkWrite(ops, {ordered:false});     ops = [];     print("更新条数:" + count); } 

});

if(ops.length > ){

db.order_detail.bulkWrite(ops, {ordered:false});

}
print(“更新完成,更新总条数:” + count);

NoSQLBooster for MongoDB 中跨库关联查询


本站部分内容转载自网络,版权属于原作者所有,如有异议请联系QQ153890879修改或删除,谢谢!
转载请注明原文链接:NoSQLBooster for MongoDB 中跨库关联查询

你还在原价购买阿里云、腾讯云、华为云、天翼云产品?那就亏大啦!现在申请成为四大品牌云厂商VIP用户,可以3折优惠价购买云服务器等云产品,并且可享四大云服务商产品终身VIP优惠价,还等什么?赶紧点击下面对应链接免费申请VIP客户吧:

1、点击这里立即申请成为腾讯云VIP客户

2、点击这里立即注册成为天翼云VIP客户

3、点击这里立即申请成为华为云VIP客户

4、点击这里立享阿里云产品终身VIP优惠价

喜欢 (0)
[[email protected]]
分享 (0)