Home > Enterprise >  $first command is giving null values
$first command is giving null values

Time:11-22

I have the collection for "article_titles" and there are some title with repetitions involved in it. I want to pick the one that is earliest.

{ "_id" : ObjectId("6375dbf89a70ce15b7c51172"), "title" : "Visualizing Graphs and Clusters as Maps", "year" : 2005, "name" : "amir" }
{ "_id" : ObjectId("6375dbf89a70ce15b7c5118a"), "title" : "Visualizing Dynamic Data with Maps", "year" : 2012, "name" : "amir" }
{ "_id" : ObjectId("6375dbf89a70ce15b7c511ac"), "title" : "Visualizing Cartograms: Goals and Task Taxonomy", "year" : 2015, "name" : "kobourov" }
{ "_id" : ObjectId("6375dbf99a70ce15b7c60293"), "title" : "Upward straight-line embeddings of directed graphs into point sets", "year" : 2008, "name" : "hossain" }
{ "_id" : ObjectId("6375dbf99a70ce15b7c738c7"), "title" : "Upward straight-line embeddings of directed graphs into point sets", "year" : 2010, "name" : "hossain" }

Here the name "hossain" has two same publications but in different years. I am trying to pick the first one, I am using

db.article_titles.aggregate([{$sort:{year:-1}},{$group:{_id:{title:"$title",name:"$name",year:{$first:"$year"}}}}])

I want to understand what I am doing wrong here?

CodePudding user response:

Originally your year: { $first: "$year" } statement was nested too deeply (inside of _id). Then with your second attempt you pulled it out too far. It should be inside of $group but outside of _id:

  {
    $group: {
      _id: {
        title: "$title",
        name: "$name"
      },
      year: {
        $first: "$year"
      }
    }
  }

Playground demonstration here

  • Related