[mongodb-user] Storing user friend relationships

351 views
Skip to first unread message

chris.chen21

unread,
Apr 27, 2010, 1:08:59 AM4/27/10
to mongodb-user
I'm trying to find the optimal schema for storing friend
relationships, and so far I've settled on having a collection
dedicated to the relationship data.

here is the object stored representing a relationship between two
users:
{
subjects:{
{
user:USERID,
consent:1
},
{
user:USERID,
consent:0
}
}
}

I'm wondering however, is it possible to use a MongoId as a key? That
way I could do a find({new ObjectId('...'):1}) for all of that user's
friends. I'm not sure if that's better but it seems like it would use
less space and be more concise than what I have above. I could do it
by storing the hex representation of the ObjectId but that would make
it use more space.

Does anyone have suggestions to how I should approach this?

Thanks

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Andreas Jung

unread,
Apr 27, 2010, 1:31:39 AM4/27/10
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

chris.chen21 wrote:
> I'm trying to find the optimal schema for storing friend
> relationships, and so far I've settled on having a collection
> dedicated to the relationship data.
>
> here is the object stored representing a relationship between two
> users:
> {
> subjects:{
> {
> user:USERID,
> consent:1
> },
> {
> user:USERID,
> consent:0
> }
> }
> }
>
> I'm wondering however, is it possible to use a MongoId as a key? That
> way I could do a find({new ObjectId('...'):1}) for all of that user's
> friends. I'm not sure if that's better but it seems like it would use
> less space and be more concise than what I have above. I could do it
> by storing the hex representation of the ObjectId but that would make
> it use more space.
>
> Does anyone have suggestions to how I should approach this?


huh? You can use the _id of another object directly and store it as
'other_person_id' and create in index on it. That's it.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkvWdrsACgkQCJIWIbr9KYx3EACgn2oxOuEjvxELnOXf+/vjzOTT
z6kAni89fUluqjD+MnEvSPPj0SQ/pi8L
=zmfp
-----END PGP SIGNATURE-----
lists.vcf

chris.chen21

unread,
Apr 27, 2010, 1:37:57 AM4/27/10
to mongodb-user
So if I were querying for a user's friends, how would I know whether
or not they are other_person_id or _id. Or are you suggesting I store
one copy for each user? I need friend relationships to be mutually
consented.
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkvWdrsACgkQCJIWIbr9KYx3EACgn2oxOuEjvxELnOXf+/vjzOTT
> z6kAni89fUluqjD+MnEvSPPj0SQ/pi8L
> =zmfp
> -----END PGP SIGNATURE-----
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.
>
>  lists.vcf
> < 1KViewDownload

chris.chen21

unread,
Apr 27, 2010, 1:47:56 AM4/27/10
to mongodb-user
Just to clarify, I basically would like one object representing two
user's relationship status. Could you clarify what you were
suggesting?

Thanks

Andreas Jung

unread,
Apr 27, 2010, 1:56:06 AM4/27/10
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

chris.chen21 wrote:
> Just to clarify, I basically would like one object representing two
> user's relationship status. Could you clarify what you were
> suggesting?
>

persons [_id1, _id2]
count int

You can index 'persons' and search by _id1 or _id2 (see docs).
A counter can be used for managing the consent (increment it by one if
one of the parties consents. count==2 -> friends.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkvWfHYACgkQCJIWIbr9KYwOxQCgxI4H71aF3iC0/ihGAwI6lHB3
c9IAnRbtEL1NPSyMDOx+C6jdmGd3U43e
=nxNC
lists.vcf

Scott Hernandez

unread,
Apr 27, 2010, 2:42:53 AM4/27/10
to mongod...@googlegroups.com
All keys ({key:value}) must be strings, as far I know. You can use a
string representation as you suggest and store a value of 0/1 as an
indication if the friendship is reciprocated/allowed/good. You will
not be able to index through the keys since they will all be different
keys (you would have to create an almost infinite number of indexes;
40 is the limit per collection). If you don't want to search then it
should be fine.

I would suggest storing a list of friends in each user object and
inferring consent based on weather they reciprocate (both user list
each other as friends). Or, you can store a list of friends, and
pending friend for each user. As the other consents, move the userid
from pending to the friend list.

chris.chen21

unread,
Apr 27, 2010, 4:04:20 AM4/27/10
to mongodb-user
> I would suggest storing a list of friends in each user object and
inferring consent based on weather they reciprocate (both user list
each other as friends).

I considered this. I guess I didn't think it through enough last time
but it seems like it should work no.

So then when I want to query a user's friends (among who have
reciprocated), I do this query:

db.users.find({_id:{$in:[friend ids]},"friends":myUserId});
where "friends" contains an array of user ids.

> persons [_id1, _id2]
count int

I considered something like that but I need to be able to determine
who has consented and who hasn't.

Thank you both for your replies.
> > For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.
Reply all
Reply to author
Forward
0 new messages