Monday 15 August 2011

node.js - Mongoose schema designing -


I want to create a list for a user. Basically, I try to create an object that contains the list of items with user_id and the number of items in each item.

My collection is a schema of 'item':

  var madasikema = new mangoes Sema ({name: string, image: string, effect: {attack: string, rescue: string, life: string}});  

I do not manage to prepare an inventory collection, where I can easily update the number of items and add new items.

Perhaps what you want is a list of "items", as you describe the cap Do, gloves, guns, etc.

Each item will have its own ID, then you must reference that item in your user inventory schema:

  var UserInventorySchema = new schema ({userId: {type: Schema.Type.ObjectId, Reference: 'User'}, Item: [{_id: {type: Schema.Type.ObjectId, ref: 'item' // This is the name for you Item Schema}, Amount: {Type: Number , Default: 0}}]});  

The same is the case that you will be more written in the database and will read less, if you are studying more then I recommend that you want to be on the same model, but Just by adding names in such a way you will not populate all the "item" data.


No comments:

Post a Comment