Focus on IT Recommend

Home > node.js - Is it possible for Mongoose to automatically extract schemas from Mongodb?

node.js - Is it possible for Mongoose to automatically extract schemas from Mongodb?



I'm still learning Mongodb, Nodejs, and Mongoose, so please excuse my ignorance if this question lacks understanding.

I find it somewhat redundant that each Mongodb collection have to be dissected in Mongoose. Specifically, all the fields of each Mongodb collection and their types need to be stated in Mongoose's schema.

So if I have a collection that contains documents sharing the same fields, such as:

> db.people.find()
    { "_id" : ObjectId("1111"), "name" : "Alice", "age": 30 }
    { "_id" : ObjectId("2222"), "name" : "Bob", "age": 25 }
    { "_id" : ObjectId("3333"), "name" : "Charlie", "age": 40 }

The way that Mongoose+Nodejs connect to this Mongodb

var mongoose = require('mongoose');
var personSchema = new mongoose.Schema({
    name     : String,
    age      : Number
mongoose.model("Person", personSchema, 'people');

where the last line contains the collection name as the 3rd parameter (explained here).

Is it possible to have Mongoose automatically extract the schema somehow from a Mongodb collection for a collection that contains documents of identical fields (i.e. they would have the same schema)? So that we don't have to define the schema in Mongoose.

node.js mongodb mongoose
  this question
edited Dec 6 '13 at 5:44 asked Dec 6 '13 at 5:20 Alice 521 2 7 17      i dont think its possible to retrieve schema from mongodb because each document in a collection can have different schema –  thejus_r Dec 6 '13 at 5:26      Thanks for pointing this out, I should emphasize that I'm only asking about the case where the collection contains document sharing the same fields. –  Alice Dec 6 '13 at 5:43      Why would you want to use Mongoose if you don't want to use schema? –  robertklep Dec 6 '13 at 6:07      @Alice You can manually insert any document into a collection. Mongo is schemaless. Thus you cannot reliably achieve this. Of cource you could fetch first document and do analysis based on that, however Mongoose does not support that. –  freakish Dec 6 '13 at 6:37      If you don't like to use the schema definition, just use the mongodb nodejs native directly. It's schema-less. –  WiredPrairie Dec 6 '13 at 12:04  |  show more comment

1 Answers


Mongoose does not currently have a way of automatically building a Schema and Model given an example document.

While a simple document to Schema tool could be written and it would handle some cases reasonably well, depending on the nature of the collections and documents in your database, it wouldn't accurately reflect various aspects of the data model.

For example, if you had two collections that were related:

var personSchema = Schema({
  _id     : Number,
  name    : String,
  age     : Number,
  stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]


Recommend:node.js - monk vs mongoose for Mongodb

using either Monk or Mongoose. Are these two libraries equivalent Do they have the same features or do they each have a specific purpose As a beginner with NodeJS, which should I use Here are some examples of code that uses Monk : var

var storySchema = Schema({
  title    : String
  author   : String

As you can see the stories field is an array of ObjectIds that are associated with the story collection. When stored in the MongoDB collection, it would be something like:

    "_id" : ObjectId("52a1d3601d02442354276cfd"),
    "name" : "Carl",
    "age" : 27,
    "stories" : [

And stories:

    "_id" : ObjectId("52a1d33b1d02442354276cfc"),
    "title" : "Alice in Wonderland",
    "author" : "Lewis Carroll"

As you can see, the stories array contains only an ObjectId without storing what it maps to (a document in the stories collection). One functionality of Mongoose that's lost without this connection being established in the schema is populate (reference).

Maybe more importantly, part of the benefit of using Mongoose is to have a declared schema. While it may be "NoSQL" and MongoDB allows documents to be schema-less, many of the drivers in fact encourage developers to have a schema as it helps enforce a consistent document structure in a collection. If you're doing "production" development, having a declared rathered than inferred schema just seems prudent to me. While you can use a design document, having a rigid Schema defined in source code makes it not only the design, but also helps to enforce the Schema from being inadvertently changed.

It's quite easy to declare a Schema in Mongoose and it only needs to be done once per application instance.

You can of course use the underlying driver for MongoDB on NodeJS which doesn't have schema support at all.

  this answer
edited Dec 6 '13 at 13:48 answered Dec 6 '13 at 13:42 WiredPrairie 37.9k 6 66 99      Just to clarify the ref field: "The ref option is what tells Mongoose which model to use during population, in our case the Story model." ~ population reference. Thanks for this information-packed answer! I wish I could have up voted twice! –  Alice Dec 6 '13 at 16:12      Yes, the ref attribute is needed to make populate work. –  WiredPrairie Dec 6 '13 at 16:18


Recommend:node.js - Multiple Schemas in a MongoDB (using Mongoose)

uation where I'm looking to have two set's of records for the game: 1. Users 2. Leader Board The user info covers things like username, password, number of game-plays and the user's top 5 scores. I currently have a schema set up for this an

------splitte line----------------------------