Node.js 使用NodeJS和Mongoose查询嵌入式文档

Node.js 使用NodeJS和Mongoose查询嵌入式文档,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我需要从mongodb查询以下数据: 项目有很多区域,一个区域有很多链接 以下是数据: { "_id" : ObjectId( "4f26a74f9416090000000003" ), "description" : "A Test Project", "regions" : [ { "title" : "North America", "_id" : ObjectId( "4f26a74f9416090000000004" ), "links" :

我需要从mongodb查询以下数据: 项目有很多区域,一个区域有很多链接

以下是数据:

{ "_id" : ObjectId( "4f26a74f9416090000000003" ),
  "description" : "A Test Project",
  "regions" : [ 
    { "title" : "North America",
      "_id" : ObjectId( "4f26a74f9416090000000004" ),
      "links" : [ 
        { "title" : "A Really Cool Link" } ] }, 
    { "description" : "That Asia Place",
      "title" : "Asia",
      "_id" : ObjectId( "4f26b7283378ab0000000003" ),
      "links" : [] }, 
    { "description" : "That South America Place",
      "title" : "South America",
      "_id" : ObjectId( "4f26e46b53f2f90000000003" ),
      "links" : [] }, 
    { "description" : "That Australia Place",
      "title" : "Australia",
      "_id" : ObjectId( "4f26ea504fb2210000000003" ),
      "links" : [] } ],
  "title" : "Test" }
以下是我的模型设置:

// mongoose
var Link = new Schema({
    title       : String
  , href        : String
  , description : String
});

var Region = new Schema({
    title       : String
  , description : String
  , links       : [Link]
});

var Project = new Schema({
    title       : String
  , description : String
  , regions       : [Region]
});

mongoose.model('Link', Link);
mongoose.model('Region', Region);
mongoose.model('Project', Project);

var Link    = mongoose.model('Link');
var Region  = mongoose.model('Region');
var Project = mongoose.model('Project');
我正在处理的查询返回一个ID匹配的区域对象。我可以返回所有区域及其各自的链接,但将其限制为仅一个区域的ID是一个问题

以下是我正在处理的坏路线:

app.get('/projects/:id/regions/:region_id', function(req, res){
  Project.findById(req.param('id'), function(err, project) {
    if (!err) {
      project.regions.findById(req.params.region_id, function(err, region) {
        if (!err) {
          res.render('project_regions', {
            locals: {
              title: project.title,
              project: project
            }
          });
        }
      });
    } else {
      res.redirect('/')
    }
  });
});
我知道上面的方法行不通,因为“findById”返回的对象不响应findById,但它说明了我要做的事情。我尝试进行自定义查询,但它总是返回整个文档,而不是我想要的单个区域

我还尝试直接从区域模式查询,但这不会返回任何结果。我假设要从作为子文档的模式中进行查询,我必须在上下文中提供父文档是什么

换句话说,以下内容不会返回包含数据的“区域”对象:

app.get('/projects/:id/regions/:region_id', function(req, res){
  Region.findById(req.params.region_id, function(err, region) {
    if (!err) {
      res.render('project_regions_show', {
        locals: {
          title: "test",
          region: region
        }
      });
    } else {
      res.redirect('/')
    }
  });
});

感谢您的帮助。

我建议使用“projectd:ObjectId()”字段将区域提取到单独的集合中。这可能会为您提供所需的查询灵活性。

bstar

您可以使用查询阻止加载子文档。选择:


这是最有意义的。我没有意识到文档查询总是返回整个文档。谢谢。我必须去掉区域模型并通过ObjectId引用它。此票证解释了我使用的填充功能:
Entity.find({ ... })
    .select({childentities: 0}) //excludes childentities
    .exec(function(err, entities){
        ...
        res.json({entities: entities});
    });