0

I need to filter this query so that documents with value Undefined does not appear. Currently, my code shows this result:

db.getCollection("actors").find({})

db.actors.aggregate([
    {
        $group: { _id: "$cast", Peliculas: { $sum: 1 } },
    },
    { $sort: { Peliculas: -1 } },
    { $limit: 2 }
])

enter image description here

I need some filter that makes Undefined disappear so that it shows me as the first value: Harold Lloyd.

I have previously performed the following operation:

//unwind
    db.getCollection("Tarea").find({})
    fase1 = { $unwind: "$cast"}
    etapas = [fase1]
    db.Tarea.aggregate( etapas )

    //out
    fase1 = { $unwind: "$cast" }
    query2 = { "_id": 0 }
    fase2 = { $project: query2 }
    fase3 = { $out: "actors" }
    etapas = [ fase1, fase2, fase3 ]
    db.Tarea.aggregate( etapas )

And now I have a new collection called 'actors'.

Adrián
  • 111
  • 10

3 Answers3

1

Try this..

Sample live demo

db.collection.aggregate([
  {
    $match: {
      cast: {
        $exists: true,
        $not: {
          $size: 0
        }
      }
    }
  },
  {
    $group: {
      _id: "$cast",
      Peliculas: {
        $sum: 1
      }
    }
  },
  {
    $sort: {
      Peliculas: -1
    }
  },
  {
    $limit: 2
  }
])

Reference

Mongodb - $match

Mongodb - $size

Mongodb - $exists

hbamithkumara
  • 2,344
  • 1
  • 17
  • 17
  • Thank you all. Maybe I haven't expressed myself well. What I need exactly is this: On actors, get a top list of the actors who have participated in more films ((t is fine to limit it to only one, since that parameter can be modified), without deleting undefined records, but filtering them so that they simply do not appear in the query. I want them to continue appearing as Undefined, but not to appear as top actors who have participated in the film. Thanks! – Adrián Dec 04 '19 at 23:24
1

@hbamithkumara answer will work perfectly if you want to eliminate all the records where cast array is empty. But if you want to also display the count Peliculas for all the empty cast array with default text like No Cast, you can try this

You can test it here

  db.collection.aggregate([
  {
    $project: {
      cast: {
        $cond: {
          if: {
            $eq: [
              "$cast",
              []
            ]
          },
          then: [
            "No Cast"
          ],
          else: "$cast"
        }
      }
    }
  },
  {
    $group: {
      _id: "$cast",
      Peliculas: {
        $sum: 1
      }
    }
  }
])

Result would be like

   [
  {
    "Peliculas": 1,
    "_id": [
      "Paul Boyton"
    ]
  },
  {
    "Peliculas": 1,
    "_id": [
      "Ching Ling Foo"
    ]
  },
  {
    "Peliculas": 21,
    "_id": [
      "No Cast"
    ]
  }
]
Shivam
  • 3,514
  • 2
  • 13
  • 27
  • I thought it is not necessary since it is mentioned *I need some filter that makes Undefined disappear* in the question. – hbamithkumara Dec 04 '19 at 19:52
  • Your answer is correct and works but it's just another option available that might come handy in some other use case. – Shivam Dec 04 '19 at 20:03
0

I finally solved it adding this line to the code:

{$match: {cast:{$ne:"Undefined"}}}

So now the complete code looks like:

db.actors.aggregate([
{$match: {cast:{$ne:"Undefined"}}},
{$group : {_id: "$cast", total : {$sum: 1 }}},
{$sort:{total:-1}},
{$limit:5}])

and I can see the top 5 actors list without Undefined:

enter image description here

Thank you all.

Adrián
  • 111
  • 10