Querying Embedded Documents with NodeJS and Mongoose

I need to query the following data from mongodb: Project has many Regions, a Region has many Links

Here’s the data:

{ "_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" }

Here’s my model setup:

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

The query that I’m struggling with is returning a single region object matched on ID.I’m fine returning all of the regions with all of their respective links, but limiting it to the ID of just one region has been a problem.

Here’s my broken route that I’m working with:

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('/')
    }
  });
});

I know the above will not work because the object returned by “findById” does not respond to findByID, but it illustrates what I’m trying to do. I tried doing a custom query, but it always returned the entire document, not the single Region that I wanted.

I also tried querying directly off the Region Schema, but that is not returning any results. I assume to query from a Schema that’s a subdocument I would have to contextually provide what the parent document is.

In other words, the following does not return an “region” object with data:

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('/')
    }
  });
});

Appreciate any help.

Problem courtesy of: bstar

Solution

I’d recommend extracting the regions into a separate collection with a “projectId: ObjectId()” field. That may give you the querying flexibility you’re looking for.

Solution courtesy of: Yuriy Nemtsov

Node.js Recipes责编内容来自:Node.js Recipes (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Querying Embedded Documents with NodeJS and Mongoose

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录