Search code examples
mongodbnestjsnode.js-typeorm

how i can update inventory with typeORM, nestjs and mongodb


how can I execute this kind of query with MongoDB typeORM

return await this.inventoryRepository.update(
      { id },
      { $inc: { totalInventory: -1 } },
    );

same query is working in the mongodb shell :

db.inventory.findOneAndUpdate({id:"someidhere"},{ $inc: {totalInventory: -1}});

but in nextjs service it is giving me an error :

Argument of type '{ $inc: { totalInventory: number; }; }' is not assignable to parameter of type 'QueryDeepPartialEntity<Inventory>'.

Solution

  • i mange this with the following code :

    async updateInventory(id: string, dif: number) {
        const manager = getMongoManager();
    
        try {
          return await manager.findOneAndUpdate(
            Inventory,
            { id },
            { $inc: { totalInventory: dif } },
          );
        } catch (err) {
          console.log(err);
        }
      }