【问题标题】:How to read nest array elements from mongoose schema using nodesjs?如何使用节点 js 从猫鼬模式中读取嵌套数组元素?
【发布时间】:2020-12-05 03:53:04
【问题描述】:

她是我的猫鼬用户模式代码:

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);

这是以json格式存储的mongodb数据:

{
    "_id" : ObjectId("5f378bec5bd30f3248ffae59"),
    "email" : "vikasthapar95@gmail.com",
    "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"
                }
            ]
        }
    ],
}

现在我想要的是仅迭代位于 Addtasks 数组内的 Bigpaths 嵌套数组,并从中获取其通过循环的每个对象的数据。我怎样才能做到这一点?我尝试过一种 populate() 的东西,但似乎没有帮助。请帮忙!!

期望的结果:

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

]

UI 端(车把)的预期结果:

Path 1 = public\\files\\chrome.VisualElementsManifest.xml
Path 2 = public\\files\\chrome_proxy.exe
Path 3 = public\\files\\master_preferences

【问题讨论】:

  • 您能否在问题中添加预期结果
  • 更新了预期结果。 @turrivisal。

标签: node.js arrays mongodb express mongoose


【解决方案1】:

这将结合根目录下的所有文档路径,

  • $unwind解构Addtasks数组
  • $unwind解构Addtasks.Bigpaths数组
  • $replaceRoot 将在新根目录中替换 path: Addtasks.Bigpaths.path
db.collection.aggregate([
  { $unwind: "$Addtasks" },
  { $unwind: "$Addtasks.Bigpaths" },
  {
    $replaceRoot: {
      newRoot: { path: "$Addtasks.Bigpaths.path" }
    }
  }
])

Playground


用于访问文档中的项目,

let paths = [];
data.Addtasks.forEach(function(Addtasks){
    Addtasks.Bigpaths.forEach(function(Bigpaths){
        paths.push({name: Bigpaths.name});
    });
})
console.log(paths);

【讨论】:

  • 但是如何访问这些项目?
  • 这里我看了。 stackoverflow.com/questions/17794398/…如何访问Tag数组?
  • 我没找到你,哪个标签数组?
  • stackoverflow.com/questions/17794398/… 检查此链接。如何访问 Images 数组中的 Tags 数组?
  • 好的,现在我对您的原始问题感到困惑,您需要准备一个查询以仅获取项目,然后这是查询,其次您已经有数据并希望项目循环通过?
猜你喜欢
  • 2014-08-13
  • 2018-10-30
  • 2020-04-25
  • 2021-04-18
  • 2015-12-18
  • 2019-03-22
  • 1970-01-01
  • 2019-02-04
  • 2022-01-25
相关资源
最近更新 更多