thread = new mongoose.Schema({
post: {type: Schema.Types.ObjectId, ref: 'Post'},
comment: {type: Schema.Types.ObjectId, ref: 'Comment'},
})
但事实上,线程只能有帖子或评论,它不能同时具有两者。
所以理想的定义应该像枚举
thread = new mongoose.Schema({
data: {type: Schema.Types.ObjectId, ref: 'Post'} OR?? {type: Schema.Types.ObjectId, ref: 'Comment'},
})
我如何在Mongoose中定义这种东西?
或者做这种事的正确方法是什么?
答案 0 :(得分:1)
你想要的是歧视者。这当然实际上是指相同的"核心"模型,但它以特殊的方式处理,以便区分不同类型的对象,甚至认为它们有自己的模型。
作为使用的示例列表:
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
值会自动添加到查询条件(当然还有对象创建)。在我们询问"评论"的情况下。要包含对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
,那么它将获得coments模式的所有附加好处。或者,如果它是Post
,那么您会看到相同的第一类对象处理。
所以无论你想用什么方式,一旦用鉴别器定义,那么mongoose会为你解决。