[英]Mongoose, define a OR ref in schema(enum)?
thread = new mongoose.Schema({
post: {type: Schema.Types.ObjectId, ref: 'Post'},
comment: {type: Schema.Types.ObjectId, ref: 'Comment'},
})
但實際上,線程只能有帖子或評論,不能同時有。
所以理想的定義應該類似於 enum
thread = new mongoose.Schema({
data: {type: Schema.Types.ObjectId, ref: 'Post'} OR?? {type: Schema.Types.ObjectId, ref: 'Comment'},
})
我如何在貓鼬中定義這種東西?
或者做這種事情的正確方法是什么?
你想要的是鑒別器。 這當然實際上指的是同一個“核心”模型,但它以一種特殊的方式處理,以便區分不同類型的對象,甚至認為它們有自己的模型。
作為使用清單的示例:
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"
選項集,您可以看到貓鼬實際上是如何發出查詢的。 因此,當您只想處理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
,那么它會獲得所有Comment
模式的附加好處。 或者,如果它是Post
,那么您會看到相同的第一類對象處理。
因此,無論您想以何種方式使用它,一旦使用鑒別器進行定義,貓鼬就會為您解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.