Monday, 22 October 2018

Updating a doc based on the value of an array element in the doc and the _id of the doc itself

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:

  1. Update the validated property of a particular Ticket in the ticket array of the TicketPurchase from true/false.

  2. Decrement the total_quantity property of the same ticket from 1. in the master Event table.(all tickets in a TicketPurchase are copies of tickets in the master Event table)

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:

  1. Checked the docs
  2. Spent some time on S/O and this proved to be the most relevant answer, but I can't get this solution to work.
  3. Interchanged my usages of id and _id, putting operators like $set in 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.';
                }


   }



from Updating a doc based on the value of an array element in the doc and the _id of the doc itself

No comments:

Post a Comment