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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:07:31+00:00 2026-05-27T20:07:31+00:00

Problem: I’m making a web service that allows developers to ‘register’ their Mongoose schemas

  • 0

Problem: I’m making a web service that allows developers to ‘register’ their Mongoose schemas – they’ll create JSON schemas in the browser, which will then be AJAX’ed to my server.

So I’ll create a new Mongoose model using the submitted schema, but now I need this schema to be existent even when I restart the server.

I have code that does something like:

/* schemaObj is what you create a schema with,
   for example { name: String, id: Number } */

registerSchema = function(model_name, schemaObj) {
    desiredSchema = new Schema(schemaObj);
    desiredCollection = 'collection_' + model_name;
    mongoose.model(desiredCollection, desiredSchema, desiredCollection);
}

Ok, so this created a model for us with the custom schema.

Now my problem is that after I run this code, create a model, and then restart the server, the new collection/model wouldn’t exist – because apparently a collection is created only when a document is saved – and I don’t want to save a document right now (because I don’t have any), just create a collection.

Question: Is there a way to create a collection with this ‘fixed’ schema – so that future docs that will be inserted should strictly conform to the schema? I’m basically looking for something like create table in SQL.

  • 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-27T20:07:31+00:00Added an answer on May 27, 2026 at 8:07 pm
    1. You cannot create a collection with a fixed schema in Mongo. This is explicitly a feature.
    2. As long as you’ve registered your new Schema with Mongoose before you try to use it, it should work just fine, whether or not the collection exists.
    3. Mongoose schema’s are only sort-of enforced anyway, you can still sneak stuff by dropping down to the native driver, which is really easy. Whatever.collection.insert
    4. You need to re-register your schemas EACH TIME you start the server
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: We have a web app that calls some web services asynchronously (from the
Problem: Trying to create a Mix that is applied to the AVPlayerItem, but it
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: Ajax suggest-search on [ n ] ingredients in recipes. That is: match recipes
Problem Language: C# 2.0 or later I would like to register context handlers to
Problem: I'm working on a website where there is a dial that displays a
Problem Description: We have a service which has applications for main mobile OS’s. We
Problem: I have 2 classes, DB class and a User class, that will work
Problem: Visitors open the url website.com/?i=133r534|213213|12312312 but this url isn't valid anymore and they

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.