Javascript 如何获取在另一个模型中定义的猫鼬数据库的架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8730255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:13:57  来源:igfitidea点击:

How to get Schema of mongoose database which defined in another model

javascriptnode.jsmongodbmongoosenosql

提问by Huy Tran

This is my folder structure:

这是我的文件夹结构:

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

My code in file songs.js

我在文件 Songs.js 中的代码

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 file albums.js

这是我在文件专辑.js 中的代码

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 SongSchemato be defined AlbumSchema


我怎样才能让albums.js 知道SongSchema被定义为 AlbumSchema

回答by alessioalex

You can get models defined elsewhere directly with Mongoose:

您可以直接使用 Mongoose 获取在其他地方定义的模型:

require('mongoose').model(name_of_model)

To get the schema in your example in albums.js you can do this:

要在专辑.js 中的示例中获取架构,您可以执行以下操作:

var SongSchema = require('mongoose').model('Song').schema

回答by peteallen

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

要从已注册的 Mongoose 模型中获取架构,您需要专门访问架构:

var SongSchema = require('mongoose').model('Song').schema;

回答by robertfoenix

For others not as familiar with the deeper aspects of how Mongoose works, the existing answers can be confusing.

对于不熟悉 Mongoose 工作原理的更深层次的其他人来说,现有的答案可能会令人困惑。

Here's a generalized implementationexample of importing a schemafrom another file that is accessible to a wider audience coming from a more general context.

这是从另一个文件中导入模式通用实现示例,该文件可供来自更通用上下文的更广泛受众访问。

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

Here's a modified version for the specific casein the question (this would be used insidealbums.js).

这是问题中特定情况的修改版本(这将albums.js 中使用)。

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

From this, I can see that you first access and require the file how one would normally go about requiring a model, except in this case you now specifically access the schema of that model.

从这里,我可以看到您首先访问并需要文件通常如何需要模型,除非在这种情况下您现在专门访问该模型的模式。

Other answers require mongoose withinthe variable declaration and that goes against the commonly found example of requiring mongoose before through declaring a variable such as const mongoose = require('mongoose');and then using mongoose like that. Such a use case was not accessible knowledge-wise to me.

其他答案需要变量声明中const mongoose = require('mongoose');使用 mongoose,这与之前通过声明变量然后使用 mongoose 之类的需要 mongoose 的常见示例背道而驰。对我来说,这样的用例在知识上是无法访问的。



Alternative option

替代选项

You can require just the modellike you normally would and then refer to the schema through the Model's schema property.

您可以像往常一样需要模型,然后通过模型的架构属性引用架构。

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]
});

回答by Amol M Kulkarni

var SongSchema = require('mongoose').model('Song').schema;

The above line of code in your albums.jswill surely work.

您上面的代码行albums.js肯定会起作用。