0

Please tell me if this is an appropriate solution to my problem:

As a manager of a franchise, you can log in. There's a list of all your employees. On the left it says:

Company Name

Store 1

  • Section 1
  • Section 2

Store 2

  • Section 1
  • Section 2

So here's an example

Bob's Grocery Stores

Chicago Store

  • Deli
  • Vegetables
  • Checkout

New York Store

  • Bakery
  • Deli
  • Vegetables
  • Checkout

So currently my idea is to have 2 models. A user model and a company model. I'm using a second company model because if the store name changes with one user, it needs to change with every user. Then I user a ref to that company within the user document.

var CompanySchema = new Schema({
    name: {
        type: String,
    },
    store: {
        type: Array
    },
 });

I would have the sections inside the 'store' property because different stores have different sections.

So far I think everything is correct. My question is how do I assign a user to a specific section in a store. Would it be user.company.store[3].section[1]? Wouldn't the indexOf values for section/store change if a section/store get deleted? How do people generally go about doing something like this? I'm basically creating the same thing as folder/file directory.

3
  • You could do a complex find like this stackoverflow.com/a/16206646/1266650 Commented Nov 15, 2014 at 20:27
  • So do I need to put every single person that's in the company inside the company object? Like should I give CompanySchema a members property or should I give UserSchema company/store/section properties? Commented Nov 15, 2014 at 22:45
  • I'm actually not entirely sure about your scenario, but from so far whatever I can understand, if you want user.company.store[3].section[1], and you're afraid the array elements' position might change, then you can instead do a complex find to search for actual company/store/section names instead of relying on array indices. Commented Nov 15, 2014 at 23:01

1 Answer 1

1

I'd do this:

Schemas:

var UserSchema = new Schema({
    name: { type: String },
    company: type: Schema.Types.ObjectId, ref: 'Company',
    stores: [{type: Schema.Types.ObjectId, ref: 'Store'}]
    sections: [{type: Schema.Types.ObjectId, ref: 'Sections'}]
})

var CompanySchema = new Schema({
    name: {
        type: String,
    },
    store:{type: Schema.Types.ObjectId, ref: 'Store'}
 });

var StoreSchema = new Schema({
    name: {
        type: String,
    },
    sections: [{type: Schema.Types.ObjectId, ref: 'Section'}]
 });

var SectionSchema = new Schema({
    name: {
        type: String,
    }
});

The value in the "ref" key is the name of the corresponding Model. So CompanySchema has a corresponding Company model.

The nice thing about actually storing object ids is you can now use 'populate' to get the references objects.

I'm storing "sections" directly inside the UserSchema for simplicity and direct access.

I'd then index the arrays in the user schema to make access even faster.

You'd have to also write some middleware to take care of removing stores/sections to ensure they're also removed from any users that reference them.

Sign up to request clarification or add additional context in comments.

4 Comments

You are an amazing person. This helped me so much, and keeps helping days later.
my pleasure! Are you the guy from uxguy.com?
I'm not! I feel bad taking his user name... I actually go by 'UX Dork' a lot. Feel free to ask me any design/ux questions :) I'm kind of a nerd like that
ah ok - i know him and was wondering why he didn't say hi. You prevented a beatdown :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.