0

I would like to design an inventory for a user. Basically,I try to create an inventory, with a user_id and a list of items with the number of each items.

There is the schema of my collection 'Item' :

 var ItemSchema = new mongoose.Schema({
    name: String,
    image: String,
    effects : {
        attack: String,
        defense: String,
        life: String
       }
    });

I don't manage to design an inventory collection where I can easily update the number of each items and add new items.

Bob
  • 589
  • 1
  • 11
  • 25
  • 1
    I'm looking for a good schema which represents a kind of inventory for a user. I have my 'ItemSchema' but I don't see how to design my InventorySchema. I want an array of ItemSchema but with a number. For example if I have two "hats', I don't want to have the Item named 'hat' two times in my collections but a field 'number' sets at '2' – Bob Mar 17 '15 at 18:12
  • any luck with my answer? – pedrommuller Mar 19 '15 at 01:43

1 Answers1

1

Probably what you would want is to have a catalogue for "Items" like the ones that you described hat, gloves, guns, etc.

Each item will have its own Id, then you'll have to reference that item into your user inventory Schema:

var UserInventorySchema = new Schema({
        userId:{ type:Schema.Types.ObjectId,
               ref: 'User'
        },
        items:[{
        _id:{
            type:Schema.Types.ObjectId,
            ref: 'Item' // this is the name for your Item Schema 
        },
        Amount:{
            type: Number,
            default:0
        }
    }]
});

that's the case that you'll have more writes to the database and fewer reads, if you are having more reads I'd suggest you to stick to the same model but just by adding the name that way you won't do a populate to bring back all the "item" data.

pedrommuller
  • 15,741
  • 10
  • 76
  • 126
  • Thanks for your answer. I wrote this code in order to uptade my "Inventory" : `Inventory.update( {'userId': req.user._id}, {$addToSet: { "items": {_id: req.body._id, amount: 1}}}, {upsert: true}, function(err, inventory) { if (err) res.status(400).send('Error update inventory); else res.status(204).send(); }); };` I can add an item in my inventory easily but I have a problem concerning the 'amount'. I would like be able to increase or decrease this amount. – Bob Mar 20 '15 at 00:50
  • Sure no problem I'm glad you nailed it – pedrommuller Mar 20 '15 at 00:54
  • Do you have an idea how to change to amount without duplicate the data in the array? – Bob Mar 20 '15 at 09:05
  • Use the ".$." inside a $set, take a look to this http://stackoverflow.com/questions/15691224/mongoose-update-values-in-array-of-objects – pedrommuller Mar 20 '15 at 13:50
  • Almost .... `Inventory.update( {'id_user': req.user._id, 'items._id': req.body._id}, {$set: { "items.$._id": req.body._id}, $inc: {"items.$.amount": 1}}, {upsert: true}, function(err, inventory) { console.log(inventory); if (err) res.status(400).send(Error.setError('impossible to put in inventory', err)); else res.status(204).send(); }); };` Now I can increase or decrease the amount ... But I have an error if the item doesn't exist in the Inventory – Bob Mar 20 '15 at 14:56
  • try to set a default value for the array in the model as a "empty array" – pedrommuller Mar 20 '15 at 15:38
  • I don't manage to add a default value in my model. Why adding a default value could help ? – Bob Mar 20 '15 at 16:22