在Mongo中的多对多连接建模数据? [英] Modeling data on a many-to-many join in Mongo?

查看:109
本文介绍了在Mongo中的多对多连接建模数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在关系数据库中,我可能有两个表,'User'和'Event',它们具有多对多关系,因此连接表'UsersEvents'说。现在我有一些数据,我想存储在这个表,而不是2 ID,类似一个布尔值称为享受。

So in a relational DB I might have 2 tables, 'User' and 'Event', which have a many-to-many relationship, and thus a join table 'UsersEvents' say. Now I have some data that I want to store on this table other than the 2 IDs, something like a boolean called 'Enjoyed'.

我知道在蒙戈,你会在表之间创建嵌入式链接使用Mongoose

I understand that in Mongo you would create embedded links between the tables e.g. using Mongoose

var Person = new Schema({
    email: String,
    events: [EventFeedback]
})

var Event = new Schema({ ... });

var EventFeedback = new Schema({
    person: Schema.ObjectId,
    event: Schema.ObjectId,
    enjoyed: Boolean
});

但这是真的最好的方式来建模吗?我想以为EventFeedback中的所有东西都可以在Person.events,它将由Event._id索引?或者这只是Mongoose的限制?

but is this really the best way to model this? I would have thought that everything in EventFeedback could be in Person.events, which would be indexed by the Event._id? Or perhaps this is just a limitation of Mongoose?

推荐答案

/ p>

You would be better off using an Embedded Document rather than a link.

var Person = { 
   email: "joe.user@users.mongodb.org",
   events: [ 
        {code: "MONGONY2012", name: "Mongo NY", date: "5/23/2012", link: "http://www.10gen.com/events/mongo-nyc", enjoyed: true}, 
        {code: "MONGOPHL2012", name: "Mongo Philly", date: "4/9/2012", link: "http://www.10gen.com/events/mongodb-philly", enjoyed: true}
   ]
}

使用点符号:

db.people.find({events.code:MONGONY2012}) code>

db.people.find({"events.code": "MONGONY2012"})

这篇关于在Mongo中的多对多连接建模数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆