2016-09-27 52 views
0

所以我一直在做一个网站,有评论部分,消息,个人资料和购物的用户。我一直想知道关于制造模式为这些功能的时候,是它更好地都在一个模式像猫鼬模式,有不同的任务有一个或有几个?

userSchema { 
    name: String, 
    .... 
    .... 
} 

或让他们单独像

userSchema { 
} 

commentSchema { 

} 

gallerySchema { 

} 

回答

1

没有人可以给你明确的答案对此,每个人都有不同的看法。

基本上,这取决于你的项目的可扩展性

当我看到这个项目

您可以创建一个单一的架构,并使用它作为嵌入形式的要求,但它不是一个很好的主意如果你正在扩展应用程序。

我的建议是为所有任务创建单独的模式,这些模式将很容易调试,缩放应用程序和以可读形式。

编辑

如果要创建独立的模式,并希望将它们连接,那么你可以使用populateObjectId

的基础上,请参阅该文档以populate collections

var mongoose = require('mongoose') 
    , Schema = mongoose.Schema 

var personSchema = Schema({ 
    _id  : Number, 
    name : String, 
    age  : Number, 
    stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }] 
}); 

var storySchema = Schema({ 
    _creator : { type: Number, ref: 'Person' }, 
    title : String, 
    fans  : [{ type: Number, ref: 'Person' }] 
}); 

var Story = mongoose.model('Story', storySchema); 
var Person = mongoose.model('Person', personSchema); 

人口

Story 
.findOne({ title: 'Once upon a timex.' }) 
.populate('_creator') 
.exec(function (err, story) { 
    if (err) return handleError(err); 
    console.log('The creator is %s', story._creator.name); 
    // prints "The creator is Aaron" 
}); 
+0

如果我是做独立的模式,请问_id让他们之间传递,如userSchema和commentSchema用户评论帖子?比如你如何将它们连接在一起? – user3296193

+0

您可以填充以加入每个集合,请参阅我更新的示例以了解更多详细信息,请参阅用于填充http://mongoosejs.com/docs/populate.html的mongoose文档 – abdulbarik

+0

感谢您的帮助! – user3296193