Getting only needed elements form array in MongoDb Schema

Viewed 34

I want to get only those element from notes Array that match the given tag from tags array in notes.

For example : If I want to get only those notes from notes Array having lrn tag in tags array but I am getting either the whole object or only the first matching note.

My Schema :

const mongoose = require('mongoose');

const noteSchema = new mongoose.Schema({
   user : {
       type: String,
       required: true,
       unique : true
   },

   notes : {
       type : [{
            title : {
                type: String,
                required: true
            },
            tags : {
                type : [String],
                required: true
            },
            note: {
                type: String,
                required: true
            }
        }],
       required: true
   }
});

const Notebook = mongoose.model('NOTEBOOK', noteSchema);
module.exports = Notebook;

My sample data :

{ _id: ObjectId("621c3a41fa2fe3c07f43edc9"),
  user: '620c81434d8a65c1aa36e0d4',
  notes: 
   [ { title: 'Eraning',
       tags: [ 'ern', 'lrn', 'dik', 'pik', 'sik' ],
       note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.\nPrior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.\n',
       _id: ObjectId("62205a32bab7aed6315195e9") },
     { title: 'Lerningngs',
       tags: [ 'lrn', 'eat', 'drink' ],
       note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.\n',
       _id: ObjectId("6220583cbab7aed63151958b") },
     { title: 'Learning',
       tags: [ 'learn', 'eran', 'buy', 'sell' ],
       note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.Solving DSA from Maths.in and SolveMaths.org using great techniques by INDIA and RUSSIA.\n',
       _id: ObjectId("6220532dbab7aed631519531") },
     { title: 'Biology',
       tags: [ 'tissue', 'cell', 'myto', 'energy', 'glycogen', 'hydrogen' ],
       note: 'Prior to his return, Craig worked at NeXT, followed by Apple, and then spent a decade at Ariba, an internet e-commerce pioneer where he held several roles including chief technology officer.Solving DSA from Maths.in and SolveMaths.org using great techniques by INDIA and RUSSIA.\n\n',
       _id: ObjectId("621f88d4c189c6b5501c3d5f") },
     { title: 'Chemistry',
       tags: [ 'this', 'is', 'great', 'thing', 'to', 'do', 'by', 'time' ],
       note: '          Prior to his return, Craig worked at NeXT, followed by Apple, \n          and then spent a decade at Ariba, an internet e-commerce pioneer \n          where he held several roles including chief technology officer.\n',
       _id: ObjectId("621f85e9c189c6b5501c3d49") },
     { title: 'Physics',
       tags: 
        [ 'newtonlawsofmotion',
          'ktg',
          'thermo',
          'fluid',
          'mechanice',
          'bernaulii' ],
       note: 'P\' + hrg + 1/2rv^2 = constant',
       _id: ObjectId("621f8520c189c6b5501c3d3f") },
     { title: 'Maths',
       tags: [ 'trignometry', 'llp', 'continuity', 'tags', 'circle' ],
       note: 'Solving DSA from Maths.in and SolveMaths.org using great techniques by INDIA and RUSSIA.',
       _id: ObjectId("621f84d7c189c6b5501c3d37") },
     { title: 'DSA from Net',
       tags: [ 'dp', 'stack', 'queue', 'heaps', 'sorting' ],
       note: 'Solving DSA from Leetcode.in and GeeksforGeeks.org',
       _id: ObjectId("621c3a41fa2fe3c07f43edca") } ],
  __v: 7 }

1 Answers

You can use $filter to get documents of given tag as shown below:

Notebook.findOne({
  user: "620c81434d8a65c1aa36e0d4" // user ID
},
{
  notes: {
    $filter: {
      input: "$notes",
      as: "note",
      cond: {
        $in: [
          "ern", // Tag to search
          "$$note.tags"
        ]
      }
    }
  }
})

Mongo Playground

Related