Home > Net >  Saving a mongo document with mongoose returns the updated document, but didn't save in the data
Saving a mongo document with mongoose returns the updated document, but didn't save in the data

Time:04-28

My issue today is with MongoDB and mongoose in javascript. So first off I have this schema:

var playerModel = new Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Users",
  },
  class: {
    type: String,
    required: true,
  },
  level: {
    type: Number,
    default: 1,
  },
  spells: {
    type: [String],
    default: ["", "", "", "", ""],
  },
  inventory: [], <- IMPORTANT
  toolbelt: {
    axe: [{}],
    pickaxe: [{}],
    fishingrod: [{}],
    hammer: [{}],
    saw: [{}],
    sewingkit: [{}],
    knife: [{}],
    gemkit: [{}],
    food: [{}],
    potion: [{}],
  },
  gear: {
    weapon: [{}],
    head: [{}],
    chest: [{}],
    gloves: [{}],
    legs: [{}],
    shoes: [{}],
    earrings: [{}],
    ring: [{}],
    necklace: [{}],
  },
  gold: {
    type: Number,
    default: 0,
  },
  bagSize: {
    type: Number,
    default: 20,
  },
  skills: {
    type: [userSkills],
    default: defaultSkills,
  },
});

and my problem is with inventory: [].

In my code I manually push items in my array which save properly in the database, but whenever I wanna modify a field of one of my array object, it won't update it like a qty for example:

player.inventory[i].qty  = 1; //Where player is my model containing my inventory
...
player.save()
      .then((doc) => console.log("DOC", doc))
      .catch((err) => console.log("ERR", err));

and the doc returned look something like this:

...
inventory: [
    {
      _id: 626a1468cdbeced19102942d,
      slug: 'crude_campfire',
      qty: 1,
      charges: 19,
      efficiency: -0.2
    },
    { _id: 626a14e7530afad2c5c1894c, slug: 'wheat', qty: 1 },
    { _id: 626a14f1530afad2c5c1894d, slug: 'carrot', qty: 1 },
    { _id: 626a150f530afad2c5c1894e, slug: 'potato', qty: 1 },
    { _id: 626a155b530afad2c5c1894f, slug: 'red_bream', qty: 1 },
    { _id: 626a15b5530afad2c5c18950, slug: 'bone', qty: 1 },
    { _id: 626a15c9530afad2c5c18951, slug: 'clam_shell', qty: 1 },
    { _id: 626a15d5530afad2c5c18952, slug: 'stone', qty: 8 },
    { _id: 626a15df530afad2c5c18953, slug: 'taeroot', qty: 1 },
    { _id: 626a15e9530afad2c5c18954, slug: 'shiny_pendant', qty: 1 },
    { _id: 626a15fd530afad2c5c18955, slug: 'sickleweed', qty: 1 },
    { _id: 626a1625530afad2c5c18956, slug: 'ceruleaf', qty: 1 },
    { _id: 626a1dba272403ea21fb71ef, slug: 'stone', qty: 1 },
    { _id: 626a1e4030a144f8179789e0, slug: 'birch_log', qty: 22 }, <- IMPORTANT
    { _id: 626a1e72372733f90cfdc003, slug: 'tree_twig', qty: 2 },
    { _id: 626a1e9a372733f90cfdc004, slug: 'honey', qty: 2 }
  ],
...

where you can see that my birch_log qty is now at 22. But whenever I go check in the database, it is still at 8

I don't know what's going on, and I'm kind of tired right, so I'll go to bed hoping someone can save me tomorrow :)

Thanks and have a good night ! :)

CodePudding user response:

To update the object having {slug: birch_log}, you can use the following query:

player.update( {user : 123456 , inventory.slug : "birch_log" } , 
               {$inc : {"inventory.$.qty" : 1} });

instead of using save() with promise, you can use update() query with async await. It will be short and clean code.

CodePudding user response:

This answer fixed my problem

Since i'm using an "anonymous" array or whatever, mongoose doesn't know what changes to track in it so I need to tell it that it's been modified !

  • Related