0

I am completely new to mongodb and I was trying to get an object from nested array, in the above mentioned array of objects I want to access an object where Fname should be Svelte and chapterName should be Chapter 0.

    [
      {
        _id: { oid: '6393fc35a2bd7da5c33e31a9' },
        Fname: 'Svelte',
        __v: { numberInt: '0' },
        data: [
          {
            chapterName: 'Chapter 0',
            notes: [
              [
                '<h2 id="heading1">About Svelte</h2>',
                '<br>',
                '<p id="p1">Its an compiler which compiles all the code and then bundel it using any one of the bundling tools.</p>',
                '<br>',
                '<p id="p2">It is very easy to understand as it somewhat looks like vanilla javascript.</p>'
              ]
            ],
            _id: { '$oid': '6393fc35a7f6aef6477ab3eb' }
          },
          {
            chapterName: 'Chapter 1',
            notes: [
              [
                '<h2 id="heading1">onMount</h2>',
                '<br>',
                '<p id="p1">something about onMount funciton in svelte.</p>'
              ]
            ],
            _id: { '$oid': '639425eb9037e86ffc337505' }
          }
        ]
      },
      {
        _id: { oid: '6395718e137c1a3763a0fefd' },
        Fname: 'Cpp',
        __v: { '$numberInt': '0' },
        data: [
          {
            chapterName: 'Chapter 0',
            notes: [
              [
                '<br>',
                '<p id="p1">Enter your paragraph here.</p>',
                '<h2 id="heading2">Enter your heading here</h2>'
              ]
            ],
            _id: { '$oid': '6395718e15ec262729cc45be' }
          }
        ]
      }
    ]

In short, I need this object

{
            chapterName: 'Chapter 0',
            notes: [
              [
                '<h2 id="heading1">About Svelte</h2>',
                '<br>',
                '<p id="p1">Its an compiler which compiles all the code and then bundel it using any one of the bundling tools.</p>',
                '<br>',
                '<p id="p2">It is very easy to understand as it somewhat looks like vanilla javascript.</p>'
              ]
            ],
            _id: { '$oid': '6393fc35a7f6aef6477ab3eb' }
          }

What will be the query to extract this object ? Thank you so much for you help.

1 Answer 1

1

Option 1:

db.collection.find({
  "data.chapterName": "Chapter 1",
   Fname: "Svelte"
},
{
  "data.$": 1
})

Explained:

Using find/project you can filter first found object in the data array. ( Please, note if there is second object in same array mathing the criteria it will not be shown )

Playground


Option 2

db.collection.aggregate([
{
  $match: {
  "data.chapterName": "Chapter 1",
  Fname: "Svelte"
 }
},
{
 "$addFields": {
  "data": {
    "$filter": {
      "input": "$data",
      "as": "d",
      "cond": {
        "$eq": [
          "$$d.chapterName",
          "Chapter 1"
        ]
      }
    }
    }
   }
   },
   {
   $unwind: "$data"
   },
  {
  "$replaceRoot": {
    "newRoot": "$data"
   }
  }
])

Eplained:

1).Using aggregate/filter you can filter all objects matching the criteria under data array 2).Using unwind/replaceRoot you can flatten the array and have only the matching objects in final result.

Playground2

P.S. If you dont have already , having compount index on below fields will improve performance of your query:

 db.collection.createIndex("data.ChapterName":1,"Fname":1})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.