How to get a mongoose database schema that is defined in another model

This is my folder structure:

+-- express_example |---- app.js |---- models |-------- songs.js |-------- albums.js |---- and another files of expressjs 

My code in songs.js file

 var mongoose = require('mongoose') , Schema = mongoose.Schema , ObjectId = Schema.ObjectId; var SongSchema = new Schema({ name: {type: String, default: 'songname'} , link: {type: String, default: './data/train.mp3'} , date: {type: Date, default: Date.now()} , position: {type: Number, default: 0} , weekOnChart: {type: Number, default: 0} , listend: {type: Number, default: 0} }); module.exports = mongoose.model('Song', SongSchema); 

And here is my code in albums.js file

 var mongoose = require('mongoose') , Schema = mongoose.Schema , ObjectId = Schema.ObjectId; var AlbumSchema = new Schema({ name: {type: String, default: 'songname'} , thumbnail: {type:String, default: './images/U1.jpg'} , date: {type: Date, default: Date.now()} , songs: [SongSchema] }); module.exports = mongoose.model('Album', AlbumSchema); 


How can I make albums.js know SongSchema to determine AlbumSchema

+47
javascript mongodb mongoose nosql
Jan 04 '12 at 16:30
source share
4 answers

You can get models defined elsewhere directly using Mongoose:

 require('mongoose').model(name_of_model) 

To get the schema in your example in the album, you can do this:

 var SongSchema = require('mongoose').model('Song').schema 
+84
Jan 04 2018-12-12T00:
source share

To get a schema from a registered Mongoose model, you need to access the schema specifically:

 var SongSchema = require('mongoose').model('Song').schema; 
+24
Oct 18 '14 at 23:15
source share
 var SongSchema = require('mongoose').model('Song').schema; 

The above line of code in your albums.js will certainly work.

+3
Jan 02 '13 at 9:37
source share

For others unfamiliar with the deeper aspects of Mongoose, existing answers may be misleading.

Here is a generalized example of the implementation of importing a scheme from another file, which is available to a wider audience, based on a more general context.

 const modelSchema = require('./model.js').model('Model').schema 

Or modified for the case in question (this will be used inside album.js).

 const SongSchema = require('./songs.js').model('Song').schema 

From this, I see that you first access the file and request it, as is usually required for the model, except that in this case you now specifically refer to the scheme of this model.

Other answers require mongoose in the variable declaration, which contradicts the generally accepted example of mongoose requirement before the variable declaration, such as const mongoose = require('mongoose'); and then using a mongoose like this. This use case was not available to me for knowledge.




Alternative option

You can request only the model, as usual, and then access the scheme through the property of the Model scheme.

 const mongoose = require('mongoose'); // bring in Song model const Song = require('./songs.js'); const AlbumSchema = new Schema({ // access built in schema property of a model songs: [Song.schema] }); 
0
Apr 04 '19 at 23:23
source share



All Articles