0

i just want the values with year 1976 from the data

{
        "_id" : "625305190f48a6512a18220d",
        "artist" : "AC/DC",
        "albums" : [
                {
                        "title" : "High Voltage",
                        "year" : 1976
                },
                {
                        "title" : "Back in Black",
                        "year" : 1980
                },
                {
                        "title" : "Dirty Deeds Done Dirt Cheap",
                        "year" : 1976
                },
                {
                        "title" : "Highway to Hell",
                        "year" : 1979
                }
        ],
        "singers" : "Bon Scott"
}

what i tried first i insert using this statements

db.getCollection('first').insertMany([])

and then to filter out with year 1976 the following code

db.first.findOne({"albums.year":1976})

and got this output:

{
        "_id" : "625305190f48a6512a18220d",
        "artist" : "AC/DC",
        "albums" : [
                {
                        "title" : "High Voltage",
                        "year" : 1976
                },
                {
                        "title" : "Back in Black",
                        "year" : 1980
                },
                {
                        "title" : "Dirty Deeds Done Dirt Cheap",
                        "year" : 1976
                },
                {
                        "title" : "Highway to Hell",
                        "year" : 1979
                }
        ],
        "singers" : "Bon Scott"
}

why is it giving years which are not 1976 i want filter only the year which are 1976

1 Answers1

0

$project after find

db.collection.find({
  "albums.year": 1976
},
{
  "albums.$": 1
})

mongoplayground

YuTing
  • 6,555
  • 2
  • 6
  • 16