你想要的是鉴别器。这当然实际上指的是同一个“核心”模型,但它以特殊的方式处理,以便区分不同类型的对象,甚至认为有自己的模型。
作为使用示例:
var async = require('async'),
util = require('util'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/content');
mongoose.set("debug",true);
function BaseSchema() {
Schema.apply(this,arguments);
this.add({
"author": String,
"body": String,
"created": { "type": Date, "default": Date.now() }
});
}
util.inherits(BaseSchema,Schema);
var itemSchema = new BaseSchema();
var postSchema = new BaseSchema({
"title": String,
"score": Number
});
var commentSchema = new BaseSchema({
"post": { "type": Schema.Types.ObjectId, "ref": "Post" }
});
var Item = mongoose.model('Item',itemSchema),
Post = Item.discriminator('Post',postSchema),
Comment = Item.discriminator('Comment',commentSchema);
async.series(
[
// Clean data
function(callback) {
Item.remove({},callback);
},
// Insert Post
function(callback) {
async.waterfall(
[
function(callback) {
Post.create({
"title": "This post",
"author": "bill",
"body": "Whoa!"
},callback);
},
function(post,callback) {
Comment.create({
"author": "ted",
"body": "Excellent!",
"post": post
},callback);
}
],
callback
);
},
function(callback) {
console.log("All Items");
Item.find().exec(function(err,docs) {
console.log(docs);
callback(err);
})
},
function(callback) {
console.log("Just populated Comments");
Comment.find().populate('post').exec(function(err,docs) {
console.log(docs)
callback(err);
});
}
],
function(err) {
if (err) throw err;
mongoose.disconnect();
}
);
还有输出:
All Items
Mongoose: items.find({}) { fields: undefined }
[ { created: Wed Mar 16 2016 14:15:56 GMT+1100 (AEDT),
__t: 'Post',
__v: 0,
body: 'Whoa!',
author: 'bill',
title: 'This post',
_id: 56e8cfed833e67750b678d9c },
{ created: Wed Mar 16 2016 14:15:56 GMT+1100 (AEDT),
__t: 'Comment',
__v: 0,
post: 56e8cfed833e67750b678d9c,
body: 'Excellent!',
author: 'ted',
_id: 56e8cfed833e67750b678d9d } ]
Just populated Comments
Mongoose: items.find({ __t: 'Comment' }) { fields: undefined }
Mongoose: items.find({ __t: 'Post', _id: { '$in': [ ObjectId("56e8cfed833e67750b678d9c") ] } }) { fields: undefined }
[ { created: Wed Mar 16 2016 14:15:56 GMT+1100 (AEDT),
__t: 'Comment',
__v: 0,
post:
{ created: Wed Mar 16 2016 14:15:56 GMT+1100 (AEDT),
__t: 'Post',
__v: 0,
body: 'Whoa!',
author: 'bill',
title: 'This post',
_id: 56e8cfed833e67750b678d9c },
body: 'Excellent!',
author: 'ted',
_id: 56e8cfed833e67750b678d9d } ]
如果您查看清单,您会看到我们确实为 Item 创建了一个模型,它将容纳所有对象,但最有趣的事情发生在以下几行:
var Item = mongoose.model('Item',itemSchema),
Post = Item.discriminator('Post',postSchema),
Comment = Item.discriminator('Comment',commentSchema);
因此,在接下来的两个模型定义中,我们不使用 mongoose.model,而是改为使用 Item.discriminator()。这会创建将包含在 Item 中的“特殊模型”,当然每种类型都有自己的附加架构,以及与该架构相关联的任何逻辑。
由于所有数据实际上都在同一个集合中,即使我们单独使用每个模型,您也会看到每个对象都添加了一个特殊键 __t。这包含与数据实际关联的模型的注册名称。
"debug" 选项集,您可以看到 mongoose 实际发出查询的方式。因此,当您只想处理Comment 时,__t 值会自动添加到查询条件(当然还有对象创建)中。在我们要求“cmets”包含对Post 的引用的相同情况下,在查找应在Post 模型中引用的内容时应用相同类型的过滤。
这是一个非常强大的模式,也可以使用与此处演示的相同类型的继承,或者只是完全空白或单独架构定义的基础。
如果它只是一个用于“引用”此集合中数据的字段。然后在外部集合中使用Item 的基本模型具有相同的结果。
var otherSchema = new Schema({
"item": { "type": Schema.Types.ObjectId, "ref": "Item" }
});
mongoose.model("Other",otherSchema);
mongoose.model("Other").find().populate("item").exec(function(err,docs( {
// populates references with either Comment or Post
// as per their assigned __t value
});
如果引用的项目是Comment,那么它将获得评论模式的所有附加好处。或者如果是Post,那么你会看到同样的第一类对象处理。
因此,无论您想以何种方式使用它,一旦使用鉴别器定义,mongoose 就会为您解决。