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

  • Home
  • SEARCH
  • 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 8187903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:43:19+00:00 2026-06-07T02:43:19+00:00

Im pretty new to JavaScript and Mongoose. I ended with a bit weird designed

  • 0

Im pretty new to JavaScript and Mongoose. I ended with a bit weird designed JSON which I want to persist via mongoose.

This is an example of this JSON:

{name : "a_string", a1 : {something : 'a_number', something_else : 'a_number'}, a2 : {...} }

a1, a2 are variable (not always there are a1 neither a2, maybe there are others)
something, something_else are variable also, can be different identifiers -they are properties-.

Maybe its my fault designing such a weird JSON but what would be the best way of defining a Schema for it?

I have various alternatives, neither convince me:

Schema {
      myJSON : []
}

This is very ugly but now i could store my JSON as myJSON[0] = {name: “theName”…}. Ofc is the ugliest, but the most close I have found to my original data-structure.

another one

json Schema {
   name: String,
   parts: [part]
}

part Schema {
   name :  String,
   props : [prop]
}

prop Schema {
   name : String,
   value : Numeric
}

This is more pretty BUT then I find several troubles, the final JSON will have many arrays and indirections I didnt have on the original one:

{name :"a_string", parts:[{name : "a1", 
           props : [{name : "something", value: 'a_number'}]},{...}]}

is there any way of removing all these annoying arrays from the Schema¿?

UPDATE:

Finally I have solved my issue because I could adapt my datamodel a bit:

var mySchema = new Schema({
       name  : String
     , valid   : Boolean
     , parts  : {}
});

Then the “props” contains all the JSON I want (except name is put outside). This is not the way I had in mind but it works fine (I think).

  • 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-06-07T02:43:22+00:00Added an answer on June 7, 2026 at 2:43 am

    It looks like you are using mongoose embedded documents.

    I’m not sure if embedded documents will always be wrapped inside arrays.

    Following this mongoose documentation example :

    var Comments = new Schema({
        title     : String
      , body      : String
      , date      : Date
    });
    
    var BlogPost = new Schema({
        author    : ObjectId
      , title     : String
      , body      : String
      , date      : Date
      , comments  : [Comments]
      , meta      : {
            votes : Number
          , favs  : Number
        }
    });
    

    A BlogPost can contains severals Comments, and Comments will be wrapped inside an array (just like your issue if i’m right).

    What if you get rid of that array in the schema definition ?

    Using Comments instead of [Comments] :

    var BlogPost = new Schema({
        author    : ObjectId
      , title     : String
      , body      : String
      , date      : Date
      , comments  : Comments
      , meta      : {
            votes : Number
          , favs  : Number
        }
    });
    

    I can’t try that now.

    Otherwise, I have an idea using mongoose virtual attributes, if this doesn’t work, let me know.

    Edit

    Using virtual attributes, you could do something like this :

    BlogPost
        .virtual('customComment')
        .get(function() { 
            return this.comments[0]; 
        });
    

    This will return the first Comment entry object whithout the array.

    var BlogPost = mongoose.model('BlogPost');
    
    BlogPost
        .find({ title: 'foo' })
        .populate('comments')
        .run(function (err, post) {
    
        console.log(post.customComment); // returns the first comment without array
    });
    

    Not tested, this may contains typos.

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

Sidebar

Related Questions

I'm pretty new to javascript. I have this sample table. I want to be
I'm pretty new to Javascript, so bear with me if this is obvious. Basically
I'm pretty new to Javascript, so forgive me if this is a simple question.
I want to write chess in JavaScript but I am pretty new to JavaScript.
I'm pretty new to ajax (via jQuery) and JavaScript. What I would like is
I'm pretty new to JavaScript, which I am learning on my own. I'm currently
All, I'm pretty new to JavaScript development, so this is probably a very stupid
All, I'm pretty new with JavaScript and this is my first attempt with the
Im pretty new with javascript, so im not sure how I would do this.
I'm pretty new to Javascript and I'm sure this is an easy fix, but

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.