首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行从MySql到Mongodb的查询

执行从MySql到Mongodb的查询
EN

Stack Overflow用户
提问于 2018-01-18 01:43:28
回答 1查看 31关注 0票数 0

我试图在MongoDb-3.2上运行一个查询,但是我无法得到我想要的结果。这是MySQL上的原始查询:

代码语言:javascript
复制
SELECT c.cdg,ten,IFNULL(efe,0) as efe,tcreated_at 
FROM 
(SELECT numB as cdg,COUNT(*) as ten,created_at as tcreated_at 
FROM dat_records cdr 
WHERE  LEFT(numB,@longitud)= @cod_prov AND type=4 
GROUP BY cdg ) c
LEFT JOIN
    (SELECT COUNT(*) as efe, numB,MAX(created_at) as ecreated_at 
FROM dat_records 
WHERE LEFT(numB,@longitud)= @cod_prov AND dat_records.time > 1 AND type=4 GROUP BY numB) d
ON c.cdg=d.numB AND tcreated_at=ecreated_at
WHERE ten > 40
GROUP BY c.cdg
HAVING (efe/ten)*100 <30
ORDER BY ten DESC
LIMIT 25

这就是我在MongoDB-3.2上尝试的:

代码语言:javascript
复制
I have this, but I need to include all documents who has or not "efectivas". So, I know the 3rd match is affecting results.

db.archivos_cdr.aggregate([
   {$match: {$and:[{numB:{$regex:'^7' }},{ tipo_llamada:4}]}},
   {$group : {_id : "$numB",tentativas: { $sum: 1 }}},
   {$match:{'tentativas': {'$gt': 4}}},
   {$sort:{"tentativas":-1}},
   {$lookup:{"from": "archivos_cdr","localField": "_id","foreignField": "numB","as": "efecList" } },

    { "$unwind": "$efecList" },
    { "$match": { $or:[ {"efecList.tiempo_conv": {"$gt": 0}}] }},
    {
    "$group": {
        "_id": "$efecList.numB",
        "tentativas": { "$first": "$tentativas" },
        "efectivas": { "$sum": 1 }
        }
    }

])

我有这样的结果:

代码语言:javascript
复制
{ "_id" : "123456789", "tentativas" : 5, "efectivas" : 4 }

但这是我想要的:

代码语言:javascript
复制
{ "_id" : "123456789", "tentativas" : 5, "efectivas" : 4 }
{ "_id" : "325987741", "tentativas" : 13, "efectivas" : 0 }

谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

发布于 2018-01-18 06:15:33

我有我自己的答案!我知道了!

代码语言:javascript
复制
db.dat_records.aggregate([
  {$match: {$and:[{numB:{$regex:'^7' }},{ tipo_llamada:4}]}},//WHERE  LEFT(numB,@longitud)= @cod_prov AND type=4 
  {$group : {_id : "$numB",//GROUP BY cdg
    ten: { $sum: 1 },//COUNT(*) as ten
    efec:{$sum:{$cond:[{$gt:["$tiempo_conv",0]},1,0]}},//
    efec_ner:{$sum:{$cond:[{$setIsSubset:[["$causa_fallo"],causas_efec_ner]},1,0]}}}//COUNT(*) as efe_ner 
           },
           {$match: { 'ten': {$gt: 40} }},//WHERE ten > 40
           {$sort:{"ten":-1}},//ORDER BY ten DESC
           { $project: {_id:0, 
           numeracion:"$_id",
           ten: 1, 
           efec: 1,
           efec_ner: 1, 
           asr: { $divide:[{$multiply: [ "$efec", 100 ]},"$ten"]},
           ner: { $divide:[{$multiply: [ "$efec_ner", 100 ]},"$ten"]},
           peso: { $multiply:[{$divide: [ "$ten", total_inter ]},100]}  }},
           {$match:{ $and:[ {"asr":{$lt:30}},//HAVING (efe/ten)*100 <30
{"peso":{$gt:10}}  ]}},//I had add this tent/total)>10(*100                
    ])

这就是结果(我所需要的!):

代码语言:javascript
复制
{ "ten" : 13, "efec" : 0, "efec_ner" : 13, "numeracion" : "12364567", "asr" : 0, "ner" : 100, "peso" : 10.56 }

无论如何,谢谢!我希望这对某些人有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48307013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档