Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Mongodb Schema design help c# driver
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ali  
View profile  
 More options Oct 5 2012, 9:06 am
From: Ali <itcoligr...@googlemail.com>
Date: Fri, 5 Oct 2012 06:06:33 -0700 (PDT)
Local: Fri, Oct 5 2012 9:06 am
Subject: Mongodb Schema design help c# driver

I am working on a system where mongodb has been chosen as the backend db.
The system stores adverts for cars. There are the following collections so
far:- cars - list of adverts posted by users userprofile - list of users in
the system containing names,address,phone no etc.

Now the issue I have here is that the users can be of different types e.g,
private user, dealer user, admin user etc.enter code here

My thinking is that the userprofile collection can embed details of dealers
where required and leave them empty when not in use so as an example:-

public class userprofile
{
public string Username{get;set;}
public string UserFirstname{get;set;}
....
public UserType UserType{get;set;}
public Company Company{get;set;}

}

Using this model would it be ok to decide who the user is by checking the
UserType, i.e UserType == UserType.Dealer then I can look into the Company
object otherwise ignore it.

Or would it be best to do this some other way.

Any suggestions appreciated.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options Oct 6 2012, 2:25 pm
From: Robert Stam <rob...@10gen.com>
Date: Sat, 6 Oct 2012 14:25:28 -0400
Local: Sat, Oct 6 2012 2:25 pm
Subject: Re: [mongodb-user] Mongodb Schema design help c# driver

That approach will work. If the Company property is null in C# then it will
be stored as a BSON null in the database. You could also tag it with
[BsonIgnoreIfNull] if you didn't want the null value stored in the database.

You could also set up a class hierarchy where the subclasses add properties
that are relevant to that type of user.

For example:

[BsonKnownTypes(typeof(Dealer)] // tell the driver about the known
subclasses
public class UserProfile
{
    // properties common to all types of users

}

public class Dealer : UserProfile
{
    public Company Company { get; set; }

}

When you call GetDatabase provide the root type as the document type of the
collection:

var collection = database.GetCollection<UserProfile>("users");
var dealer = new Dealer { ... };
collection.Insert(dealer);

When you save an instance of a subclass (like Dealer) to  a collection
where the default document type is a base class (like UserProfile), the
driver automatically adds what's called a discriminator to the stored
document:

{ _id : ???, _t : "Dealer", ..., Company : "Acme" }

The discriminator is stored in the element called "_t".  The ... represents
the fields common to all user types (they came from the base class).

The discriminator serves more or less the same purpose as your UserType
property, but there is harm in having both of them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ali  
View profile  
 More options Oct 7 2012, 11:07 am
From: Ali <itcoligr...@googlemail.com>
Date: Sun, 7 Oct 2012 08:07:33 -0700 (PDT)
Local: Sun, Oct 7 2012 11:07 am
Subject: Re: Mongodb Schema design help c# driver

Thanks for that.

I guess the question is now as follows, would it be a bad design decision
to use two different collections in this case as they mare me lots more
private than dealers for example, or would it be best to stick with the
inheritance approach you have mentioned.  Also ifyou mentioned the
discriminator _t and said "there is harm in having both" was that "there is
no harm is having both"?

I would rather this this right as there may be many types of users and I
want performance gain when the size increases but then again I also want a
good design that can later be switched if required.  Also if the
discriminator is there and I do not have a UserType property would I still
do
if(userprofile == dealer) to determine the user type?

Thanks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options Oct 8 2012, 12:06 pm
From: Robert Stam <rob...@10gen.com>
Date: Mon, 8 Oct 2012 12:06:31 -0400
Local: Mon, Oct 8 2012 12:06 pm
Subject: Re: [mongodb-user] Re: Mongodb Schema design help c# driver

Nothing wrong at all with using two collections if you would rather
segregate the data. That's fine.

If you omit your UserType property and are still using a polymorphic
inheritance tree and storing all the users in a single tree, you would have
to use some other way to test what type of user you just read. Probably
something like:

if (user.GetType() == typeof(Dealer)) // test the C# type of user
{
    // user is a Dealer

}

Of course if Dealers are in their own collection you wouldn't have to test
at all since you would just know that all users in that collection are
Dealers.

I did mean "no harm" rather than "harm" in my last sentence. Sorry for the
unfortunate typo!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ali  
View profile  
 More options Oct 11 2012, 7:44 am
From: Ali <itcoligr...@googlemail.com>
Date: Thu, 11 Oct 2012 04:44:30 -0700 (PDT)
Local: Thurs, Oct 11 2012 7:44 am
Subject: Re: Mongodb Schema design help c# driver

Thanks for the help I will end up using one of these ways and just see what
happens.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »