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 7680351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:05:44+00:00 2026-05-31T18:05:44+00:00

I am using mongoose/nodejs to get data as json from mongodb. For using mongoose

  • 0

I am using mongoose/nodejs to get data as json from mongodb. For using mongoose I need to define schema first like this

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var GPSDataSchema = new Schema({
    createdAt: { type: Date, default: Date.now }
    ,speed: {type: String, trim: true}
    ,battery: { type: String, trim: true }
});

var GPSData = mongoose.model('GPSData', GPSDataSchema);
mongoose.connect('mongodb://localhost/gpsdatabase');
var db = mongoose.connection;
db.on('open', function() {
    console.log('DB Started');
});

then in code I can get data from db like

GPSData.find({"createdAt" : { $gte : dateStr, $lte:  nextDate }}, function(err, data) {

            res.writeHead(200, {
                    "Content-Type": "application/json",
                    "Access-Control-Allow-Origin": "*"
            });
            var body = JSON.stringify(data);
            res.end(body);
        });

How to define scheme for a complex data like this, you can see that subSection can go to any deeper level.

[
  {
    'title': 'Some Title',
    'subSection': [{
       'title': 'Inner1',
       'subSection': [
          {'titile': 'test', 'url': 'ab/cd'} 
        ]
    }]
  },
  ..
]
  • 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-31T18:05:45+00:00Added an answer on May 31, 2026 at 6:05 pm

    From the Mongoose documentation:

    var Comment = new Schema({
        body  : String
      , date  : Date
    });
    
    var Post = new Schema({
        title     : String
      , comments  : [Comment]
    });
    

    Notice how Comment is defined as a Schema, and then referenced in the array Post.comments

    Your case is a bit different: you have a self-referencing Schema which I have not tried but it would look something like this:

    var sectionSchema = new Schema({
      title: String
      ,subSections: [sectionSchema]
    });
    
    mongoose.model('Section', sectionSchema);
    

    Then you could add subSections like so:

    var section = new mongoose.model('Section');
    section.subSections.push({title:'My First Subsection'})
    

    Let me know how that works out.

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

Sidebar

Related Questions

I'm using Mongoose, MongoDB, and Node.js. I would like to define a schema where
I am using node.js with express. I read out data from MongoDB with Mongoose
NodeJS + Express, MongoDB + Mongoose I have a JSON feed where each record
I am using mongoose with nodejs. I am using mapReduce to fetch data grouped
Recently I start using MongoDB with Mongoose on Nodejs. When I use Model.find method
I'd like to expose a REST API on my application, using the Mongoose web
I installed mongoose on node.js using npm. When I try to connect with this
I am using Node.js and Mongoose to store some data. After update, I have
I want to retrieve the last inserted _id , using mongoose as MongoDB wrapper
I am new to Node, and I'm using Mongoose as a driver for MongoDB.

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.