Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用mongoose获取/查找子集合_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 使用mongoose获取/查找子集合

Node.js 使用mongoose获取/查找子集合,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我是MongoDB的新手。我对嵌套集合有一些问题。假设我在mongodb中有3个不同的集合。(1) 课程(2)科目和(3)章节 类包含各种类名,主题包含特定类的不同主题名,章节包含特定主题的不同主题名 使用mongoose,我可以获取/查找以下章节的父集合: ChapterModel.find({}) .限额(1) .填充([ {path:“classId”,model:ClassModel}, {路径:“主体”,模型:主体模型} ]); 结果如下: [ { "_id": "5de

我是MongoDB的新手。我对嵌套集合有一些问题。假设我在mongodb中有3个不同的集合。(1) 课程(2)科目和(3)章节

类包含各种类名,主题包含特定类的不同主题名,章节包含特定主题的不同主题名

使用mongoose,我可以获取/查找以下章节的父集合:

ChapterModel.find({})
.限额(1)
.填充([
{path:“classId”,model:ClassModel},
{路径:“主体”,模型:主体模型}
]);
结果如下:

[
  {
    "_id": "5de6a660a5f6a42060d532cd",
    "name": "Physical Quantities and Their Measurements",
    "subjectId": {
      "_id": "5de6a660a5f6a42060d532c5",
      "classId": "5de6a65fa5f6a42060d532c4",
      "name": "Physics"
    },
    "classId": {
      "_id": "5de6a65fa5f6a42060d532c4",
      "name": "IX"
    }
  }
]
但我找不到任何方法以以下方式获取类的子级:

[
  {
    "_id" : ObjectId("5de6a660a5f6a42060d532c5"),
    "name": "IX",
    "subjects": [
      {
        "_id" : ObjectId("5de6a660a5f6a42060d532c6"),
        "name": "Physics",
        "classId": ObjectId("5de6a660a5f6a42060d532c5"),
        "chapters": [
          {
            "_id" : ObjectId('5c09fb04ff03a672a26fb23a'),
            "name": Physical Quantities and Their Measurements",
            "classId": ObjectId("5de6a660a5f6a42060d532c5"),
            "subjectId": ObjectId("5de6a660a5f6a42060d532c6"),
          },
          {
            "_id" : ObjectId("5de6a660a5f6a42060d532c6"),
            "name": "Motion",
            "classId": ObjectId("5de6a660a5f6a42060d532c5"),
            "subjectId": ObjectId("5de6a660a5f6a42060d532c6"),
          }
        ]
      }
    ]
  }
]
它们是否像“填充”一样查找所有嵌套子对象或集合?

您可以使用猫鼬的功能

解决问题的一种方法是像这样设计模式和模型: (注意,我使用了课程名称而不是类,因为它在javascript中是一个保留关键字)

course.js

const mongoose=require(“mongoose”);
const courseSchema=newmongoose.Schema(
{
名称:String
},
{
toJSON:{virtuals:true}
}
);
courseSchema.virtual(“主题”{
参考:“主题”,
外域:“课程”,
localField:“\u id”
});
module.exports=mongoose.model(“课程”,courseSchema);
subject.js

const mongoose=require(“mongoose”);
const subjectSchema=new mongoose.Schema(
{
名称:String,
课程:{
类型:mongoose.Types.ObjectId,
参考:“课程”
}
},
{
toJSON:{virtuals:true}
}
);
subjectSchema.virtual(“章节”{
参考:“第章”,
外域:“主题”,
localField:“\u id”
});
module.exports=mongoose.model(“Subject”,subjectSchema);
第6.js章:

const mongoose=require(“mongoose”);
const chapterSchema=新的mongoose.Schema({
名称:String,
主题:{
类型:mongoose.Types.ObjectId,
参考:“主题”
}
});
module.exports=mongoose.model(“章节”,章节模式);
请注意,virtual关键字的意思是。 我选择了这个解决方案,因为否则我们应该在课程模式中保留一个名为subjects的附加字段,在这里我们需要保留一个subject id数组(以及subject模式中的chapters字段)

使用这些模式,您可以使用以下查询来获得所需的结果:

const-Course=require(“../models/Course”);
路由器.get(“/courses”,异步(请求,res)=>{
const result=await Course.find({}).populate({
路径:“主题”,
填充:{path:“chapters”}
});
res.send(结果);
});
如果你很难测试,以下是创建课程、主题和章节的其他途径:

const Subject=require(“../models/Subject”);
const Chapter=require(“../models/Chapter”);
const Course=require(“../models/Course”);
路由器.post(“/courses”,异步(请求、回复)=>{
const result=等待课程创建(请求正文);
res.send(结果);
});
路由器.post(“/subjects”),异步(请求,res)=>{
const result=等待Subject.create(请求主体);
res.send(结果);
});
路由器.post(“/章”,异步(请求,回复)=>{
const result=等待章节创建(请求正文);
res.send(结果);
});
路由器.get(“/courses”,异步(请求,res)=>{
const result=await Course.find({}).populate({
路径:“主题”,
填充:{path:“chapters”}
});
res.send(结果);
});
首先,我用这个主体创建了一个帖子:(给出课程id 5de6c047856efe390cbf665d)

然后我用这个5de6c047856efe390cbf665d课程id创建了一个主体:(给出主体id 5de6c0d7856efe390cbf665e)

然后使用主题id 5de6c0d7856efe390cbf665e,我创建了两个章节:

{
    "name": "Physical Quantities and Their Measurements",
    "subject": "5de6c0d7856efe390cbf665e"
}
结果是:

[
    {
        "_id": "5de6c047856efe390cbf665d",
        "name": "IX",
        "__v": 0,
        "subjects": [
            {
                "_id": "5de6c0d7856efe390cbf665e",
                "name": "Physics",
                "course": "5de6c047856efe390cbf665d",
                "__v": 0,
                "chapters": [
                    {
                        "_id": "5de6c123856efe390cbf665f",
                        "name": "Physical Quantities and Their Measurements",
                        "subject": "5de6c0d7856efe390cbf665e",
                        "__v": 0
                    },
                    {
                        "_id": "5de6c136856efe390cbf6660",
                        "name": "Motion",
                        "subject": "5de6c0d7856efe390cbf665e",
                        "__v": 0
                    }
                ],
                "id": "5de6c0d7856efe390cbf665e"
            }
        ],
        "id": "5de6c047856efe390cbf665d"
    }
]

为什么它会生成一个额外的id字段?@RejaulKarim id value与_id的值相同,它的出现似乎是因为toJSON。但我认为这不是一个大问题。稍后我将在这里进行注释以删除该_id。@RejaulKarim在模式定义中添加id:false选项,如:
{toJSON:{virtuals:true},id:false}
{
    "name": "Physical Quantities and Their Measurements",
    "subject": "5de6c0d7856efe390cbf665e"
}
{
    "name": "Motion",
    "subject": "5de6c0d7856efe390cbf665e"
}
[
    {
        "_id": "5de6c047856efe390cbf665d",
        "name": "IX",
        "__v": 0,
        "subjects": [
            {
                "_id": "5de6c0d7856efe390cbf665e",
                "name": "Physics",
                "course": "5de6c047856efe390cbf665d",
                "__v": 0,
                "chapters": [
                    {
                        "_id": "5de6c123856efe390cbf665f",
                        "name": "Physical Quantities and Their Measurements",
                        "subject": "5de6c0d7856efe390cbf665e",
                        "__v": 0
                    },
                    {
                        "_id": "5de6c136856efe390cbf6660",
                        "name": "Motion",
                        "subject": "5de6c0d7856efe390cbf665e",
                        "__v": 0
                    }
                ],
                "id": "5de6c0d7856efe390cbf665e"
            }
        ],
        "id": "5de6c047856efe390cbf665d"
    }
]