最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongooseJS schema relation - Stack Overflow

programmeradmin4浏览0评论

I have MongooseJS schema as follow:

var UserSchema = new Schema({
    name            : String,
    app_key         : String,
    app_secret      : String,
    tasks           : [{ type   : Schema.ObjectId, 
                            ref : 'Task'}]
})

var ActionSchema = new Schema({
    name            : String,
    description     : String,
    module          : String
})

var enumTaskState = ['New', 'Indexing', 'Idle', 'In Queue', 'Working'];
var TaskSchema = new Schema({
    name            : String,
    lastPerformed   : Date,
    folder          : String,
    actions         : [{type    : Schema.ObjectId, 
                        ref     : 'Task'}],
    user            : { type    : Schema.ObjectId, 
                        ref     : 'User'},
    status          : { type    : String,
                        enum    : enumTaskState,
                        default : 'New'}
})

Problem is, when I set a task's user, do I manually have to go to the user and add a task there too? This seems like extra work (redundancy), is there an option in mongoosejs that will allow me to specify the relations it will handle everything by itself? Thanks.

I have MongooseJS schema as follow:

var UserSchema = new Schema({
    name            : String,
    app_key         : String,
    app_secret      : String,
    tasks           : [{ type   : Schema.ObjectId, 
                            ref : 'Task'}]
})

var ActionSchema = new Schema({
    name            : String,
    description     : String,
    module          : String
})

var enumTaskState = ['New', 'Indexing', 'Idle', 'In Queue', 'Working'];
var TaskSchema = new Schema({
    name            : String,
    lastPerformed   : Date,
    folder          : String,
    actions         : [{type    : Schema.ObjectId, 
                        ref     : 'Task'}],
    user            : { type    : Schema.ObjectId, 
                        ref     : 'User'},
    status          : { type    : String,
                        enum    : enumTaskState,
                        default : 'New'}
})

Problem is, when I set a task's user, do I manually have to go to the user and add a task there too? This seems like extra work (redundancy), is there an option in mongoosejs that will allow me to specify the relations it will handle everything by itself? Thanks.

Share Improve this question asked Jan 5, 2012 at 4:59 0xSina0xSina 21.6k34 gold badges143 silver badges257 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

MongoDB is not a relational database, it is a document based based database.

You can get a user's list of tasks by querying the TaskSchema and looking for the user you want. Just make sure to add an index so it will be a fast query:

user: {type: Schema.ObjectId, ref: 'User', index: true}

To elaborate on emostar's answer:

You're trying to use MongoDB like a relational database. You're giving Users a list of Tasks, but each Task is referencing a User. In a typical MongoDB schema, you'd figure out how you want to use the models in your app and just embed the documents where it makes sense (e.g. if Users contains an array of Tasks, there's no need for a task to have a reference to it's owner--just look at the User that owns the collection).

发布评论

评论列表(0)

  1. 暂无评论