English 中文(简体)
Mongodb数据库设计
原标题:Mongodb database design
  • 时间:2012-01-12 14:56:01
  •  标签:
  • mongodb

我是用户,他们可以创造活动,用户有朋友,每个用户都应能够从朋友那里获得所有活动,单一活动可以有多个用户。

Users -> _id, name, friendList
Activity -> _id, description, date, involvedUsers

举例来说,文件是:

"User theo": 1, theo, array(mike, rutger, tijmen)
"User mike": 2, mike, array(theo, rutger, tijmen)

Activity x: 1,  having fun with friends , {{todaydatetime}}, array(1, 2)
Activity y: 2,  going out , {{saturdaydatetime}}, array(1, 2)

因此,现在,如果“theo”的标志,他必须发现,Mike“与朋友们站在一起”,如果是Mike的标志的话。

如果他们选择饱和日,Mike就会发现,这个日子正在走,而Theo将获得Mike正在展开的活动。

我知道我如何做到这一点,我非常容易地在MySql加入等地这样做,但我如何能够在某个日期之前获得用户“朋友”和过滤的所有活动,例如给我所有正在经历的动荡日的活动。

最佳回答

你们可能想像这样的东西(这是完全没有测试的),而其他人则正确,如果你们的朋友名单和你们的参与范围太大(千万ous),这很可能造成更大的问题:

collection users:
{
  _id: objid, 
  name: "theo", 
  friends: [objid, objid, objid] /* these are user object ids */
}

collection activities:
{
  _id: objid, 
  desc: "having fun with friends", 
  date: new Date(), 
  involved: [objid, objid, objid] /* these are user object ids */
}

我确实会确保Index在日期和所涉时间。 http://www.mongodb.org

db.activities.ensureIndex({date:1});
db.activities.ensureIndex({involved:1});

因此,选择参与你所要进行的活动的用户(从车上):

var start_date = new Date(2012, 1, 14);
var end_date = new Date(2012, 1, 15);
var friends = db.users.findOne(_id: objid for user).friends;
var activities = db.activities.find(
   {
     date: {$gte : start_date},
     date: {$lte : end_date},
     involved : {$in : friends}
   });
问题回答

如果你不得不使用MongoDB(这是不好的选择),你必须读到该书。

对于这种情况,你有兴趣在一阵列内搜寻:





相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签