超出从MySql到Mongodb的查询

时间:2018-01-17 17:43:28

标签: mysql mongodb

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

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 }
        }
    }

])

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

{ "_id" : "123456789", "tentativas" : 5, "efectivas" : 4 }

我有这个结果:

{ "_id" : "123456789", "tentativas" : 5, "efectivas" : 4 }
{ "_id" : "325987741", "tentativas" : 13, "efectivas" : 0 }

但这就是我想要的:

<div class="options">

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我有自己的答案!!!!我明白了!!!!!

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                
    ])

这是结果(我只需要!!!!):

{ "ten" : 13, "efec" : 0, "efec_ner" : 13, "numeracion" : "12364567", "asr" : 0, "ner" : 100, "peso" : 10.56 }

非常感谢!!!!我希望这有助于某人。