1

Her is my mongoose schema code for Users:

var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
  email: {
    type: String,
    unique: true,
    required: true
  },
  name: {
    type: String,
    required: true
  },
 
  Addtasks : [{
    website: String,
    otherdetails: String,
    exampleRadios: String,
    deadline: Date,
    Date: String,
    fileName: String,
    Bigpaths:[]
  }]

});

module.exports = mongoose.model('User', userSchema);

And this is mongodb data stored in jSON format:

{
    "_id" : ObjectId("5f378bec5bd30f3248ffae59"),
    "email" : "[email protected]",
    "name" : "Vikas Yadav",
    "Addtasks" : [ 
        {
            "website" : "grumpytext.com",
            "keywords" : "article importance, article generation, article quality",
            "words" : 1234567,
            "topic" : "How article is generated?",
            "_id" : ObjectId("5f379c7164a77a338483704c"),
            "Bigpaths" : [ 
                {
                    "path" : "public\\files\\chrome.VisualElementsManifest.xml",
                    "name" : "chrome.VisualElementsManifest.xml"
                }, 
                {
                    "path" : "public\\files\\chrome_proxy.exe",
                    "name" : "chrome_proxy.exe"
                }, 
                {
                    "path" : "public\\files\\master_preferences",
                    "name" : "master_preferences"
                }
            ]
        }
    ],
}

Now what I want is that to iterate the Bigpaths nested array only which is inside Addtasks array and get the data from it for every object of its through loop. How can I achieve that? I have tried kind of populate() thing but seems doesnt help. Please help!!

Desired Result:

[
                {
                    "path" : "public\\files\\chrome.VisualElementsManifest.xml",
                }, 
                {
                    "path" : "public\\files\\chrome_proxy.exe",
                }, 
                {
                    "path" : "public\\files\\master_preferences",
                }

]

Expected Result on UI side (handlebar):

Path 1 = public\\files\\chrome.VisualElementsManifest.xml
Path 2 = public\\files\\chrome_proxy.exe
Path 3 = public\\files\\master_preferences
2
  • can you add expected result in your question Commented Aug 15, 2020 at 13:49
  • Updated the expected result. @turivisal. Commented Aug 15, 2020 at 14:12

1 Answer 1

0

This will combine all documents path in root,

  • $unwind deconstruct Addtasks array
  • $unwind deconstruct Addtasks.Bigpaths array
  • $replaceRoot will replace path: Addtasks.Bigpaths.path in new root
db.collection.aggregate([
  { $unwind: "$Addtasks" },
  { $unwind: "$Addtasks.Bigpaths" },
  {
    $replaceRoot: {
      newRoot: { path: "$Addtasks.Bigpaths.path" }
    }
  }
])

Playground


for accessing items from your document,

let paths = [];
data.Addtasks.forEach(function(Addtasks){
    Addtasks.Bigpaths.forEach(function(Bigpaths){
        paths.push({name: Bigpaths.name});
    });
})
console.log(paths);
Sign up to request clarification or add additional context in comments.

8 Comments

But how to access the items?
Here I looked upon it. stackoverflow.com/questions/17794398/… How to access the Tag array ?
i am not getting you, which tag array?
stackoverflow.com/questions/17794398/… Check this link. How to access the Tags array inside Images array ?
ok now i am confuse with your original question, you need to prepare a query to get only items, then this is the query, second you have already data and want items loop through?
|

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.