Javascript 模式中的猫鼬模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8737082/
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
Mongoose schema within schema
提问by 0xSina
How can I add a schema to another schema? This doesn't seem to be valid:
如何将架构添加到另一个架构?这似乎无效:
var UserSchema = new Schema({
name : String,
app_key : String,
app_secret : String
})
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : UserSchema
})
I checked the website and it shows how to declare it for an array but not for single.
我检查了网站,它显示了如何为数组声明它而不是为单个声明。
Thanks
谢谢
回答by glortho
There are a few ways to do this. The simplest is just this:
有几种方法可以做到这一点。最简单的就是这样:
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : Schema.ObjectId
});
Then you just have to make sure your app is writing that id and using it in queries to fetch "related" data as necessary.
然后,您只需要确保您的应用程序正在写入该 ID 并在查询中使用它来根据需要获取“相关”数据。
This is fine when searching tasks by user id, but more cumbersome when querying the user by task id:
这在按用户 id 搜索任务时很好,但在按任务 id 查询用户时更麻烦:
// Get tasks with user id
Task.find({user: user_id}, function(err, tasks) {...});
// Get user from task id
Task.findById(id, function(err, task) {
User.findById(task.user, function(err, user) {
// do stuff with user
}
}
Another way is to take advantage of Mongoose's populatefeature to simplify your queries. To get this, you could do the following:
另一种方法是利用 Mongoose 的填充功能来简化您的查询。为此,您可以执行以下操作:
var UserSchema = new Schema({
name : String,
app_key : String,
app_secret : String,
tasks : [{type: Schema.ObjectId, ref: 'Task'}] // assuming you name your model Task
});
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : {type: Schema.ObjectId, ref: 'User'} // assuming you name your model User
});
With this, your query for all users, including arrays of their tasks might be:
有了这个,您对所有用户的查询,包括他们的任务数组可能是:
User.find({}).populate('tasks').run(function(err, users) {
// do something
});
Of course, this means maintaining the ids in both places. If that bothers you, it may be best to stick to the first method and just get used to writing more complex (but still simple enough) queries.
当然,这意味着在两个地方都维护 id。如果这让您感到困扰,最好坚持使用第一种方法并习惯于编写更复杂(但仍然足够简单)的查询。
回答by Jamund Ferguson
What about this simple solution?
这个简单的解决方案怎么样?
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : {
name : String,
app_key : String,
app_secret : String
}
})
回答by Ben Henderson
As of version 4.2.0, mongoose supports single subdocuments.
从 4.2.0 版本开始,猫鼬支持单个子文档。
From the docs:
从文档:
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
// Array of subdocuments
children: [childSchema],
// Single nested subdocuments. Caveat: single nested subdocs only work
// in mongoose >= 4.2.0
child: childSchema
});