Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7407907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:48:47+00:00 2026-05-29T05:48:47+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:48:48+00:00Added an answer on May 29, 2026 at 5:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Query and i need the query to fetch data from
I currently need to execute a database query that involves data from three separate
The following query returns data right away: SELECT time, value from data order by
Is it possible to express the following SQL query in mongodb: SELECT * FROM
Greetings, Here is my problem. I need to get data from multiple rows and
I have the following data being returned from a query. Essentially I am putting
I have the following query, which I designed to compile data from a number
I have the following data being returned from my database query: +---------------+-----------+------------------+--------------+-------+ | district_name
I need a query which can do the following operation. I have a table
we need to implement the following query in such a way that we should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.