As the question states, I'm failing to get this update operation to work.
My scenario:
I have an Event, a Ticket and a TicketPurchase. The Event and TicketPurchase have Ticket arrays as properties. 
What I need to achieve:
- Update the - validatedproperty of a particular- Ticketin the ticket array of the- TicketPurchasefrom true/false.
- Decrement the - total_quantityproperty of the same ticket from 1. in the master- Eventtable.(all tickets in a- TicketPurchaseare copies of tickets in the master- Eventtable)
Almost all my experience as a programmer has been spent working with MySQL, so I am still very much a beginner in the NoSQL world.
What I have tried:
- Checked the docs
- Spent some time on S/O and this proved to be the most relevant answer, but I can't get this solution to work.
- Interchanged my usages of idand_id, putting operators like$setin and out of ' ' marks, and all other similar configurations, nothing will give.
ticket.js
const TicketSchema = new Schema({
type : {type: String},
total_quantity : {type: Number},
price : {type: String},
limit_per_order: {type: Number},
start_date: {type: Date},
end_date: {type: Date},
description: {type: String},
validated: {type: String, default: 'false'}
});
event.js
const EventSchema = new Schema({
title: {type: String},
location: {type: String},
start_date: {type: Date},
start_time: {type: String},
end_date: {type: Date},
end_time: {type: String},
description: {type: String},
organizer_name: {type: String},
organizer_about: {type: String},
cover_picture: {type: String},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
access_code: {type: String, default: shortid.generate}
});
ticketPurchase.js
const TicketPurchaseSchema = new Schema({
user: {type: Schema.Types.ObjectId, ref: 'User'},
event: {type: Schema.Types.ObjectId, ref: 'Event'},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
time_stamp: {type: Date}
});
update.js
for(var x of ticketPurchase.tickets){
                //console.log(x);
                if(x.id === ticket_id && x.validated === 'false'){
                    console.log('ticket not validated. Update');
                    TicketPurchase.update(
                        {_id: ticket_purchase_id, 'tickets._id': ticket_id},
                        {'$set':{
                            'tickets.$.validated': 'true'
                        }},
                        function (err){
                            console.log('updated validated');
                            if(err){console.log(err);}
                        }
                    );
                    Event
                    .update({_id: event_id, "tickets._id": x.id},
                    {$inc : {"tickets.$.total_quantity" : -1}});
                    console.log('updated ticket.total_qty');
                    payload['success'] = 'true';
                }else if(x.id === ticket_id && x.validated === 'true'){
                    console.log('ticket validated');
                    payload['success'] = 'false';
                    payload['message'] = 'Ticket already validated.';
                }
   }
 
    