0

I looked at other questions and I feel mine was different enough to ask.

I am sending a (potentially) large amount of information back to my backend, here is an example data set:

   [ { orders: [Array],
       _id: '5c919285bde87b1fc32b7553',
       name: 'Test',
       date: '2019-03-19',
       customerName: 'Amego',
       customerPhone: '9991112222',
       customerStreet: 'Lost Ave',
       customerCity: 'WestZone',
       driver: 'CoolCat',
       driverReq: false, // this is always false when it is ready to print
       isPrinted: false, // < this is important
       deliveryCost: '3',
       total: '38.48',
       taxTotal: '5.00',
       finalTotal: '43.48',
       __v: 0 },
     { orders: [Array],
       _id: '5c919233bde87b1fc32b7552',
       name: 'Test',
       date: '2019-03-19',
       customerName: 'Foo',
       customerPhone: '9991112222',
       customerStreet: 'Found Ave',
       customerCity: 'EastZone',
       driver: 'ChillDog',
       driverReq: false,// this is always false when it is ready to print
       isPrinted: false, // < this is important
       deliveryCost: '3',
       total: '9.99',
       taxTotal: '1.30',
       finalTotal: '11.29',
       __v: 0 },
     { orders: [Array],
       _id: '5c91903b6e0b7f1f4afc5c43',
       name: 'Test',
       date: '2019-03-19',
       customerName: 'Boobert',
       customerPhone: '9991112222',
       customerStreet: 'Narnia',
       customerCity: 'SouthSzone',
       driver: 'SadSeal',
       driverReq: false,// this is always false when it is ready to print
       isPrinted: false, // < this is important
       deliveryCost: '3',
       total: '41.78',
       taxTotal: '5.43',
       finalTotal: '47.21',
       __v: 0 } ] }

My front end can find all the orders that include isPrinted:false, I then allow the end user to 'print' all the orders that are prepared, in which, I need to change isPrinted into true, that way when I pull up a next batch I won't have reprints.

I was looking at db.test.updateMany({foo: "bar"}, {$set: {isPrinted: true}}), and I currently allow each order to set a new driver, which I update by:

Order.update({
    _id: mongoose.Types.ObjectId(req.body.id)
    },
    {
    $set: {
        driver:req.body.driver, driverReq:false
    }

which is pretty straight forward, as only 1 order comes back at a time.

I have considered my front end doing a foreach and posting each order individually, then updating the isPrinted individually but that seems quite inefficient. Is there a elegant solutions within mongo for this?

I'm not sure how I would user updateMany considering each _id is unique, unless I grab all the order's who are both driverReq:false and isPrinted:false (because that is the case where they are ready to print.

Robolisk
  • 1,682
  • 5
  • 22
  • 49

1 Answers1

0

I found a solution, that was in fact using UpdateMany.

Order.updateMany({
    isPrinted: false, driverReq:false
}, 
{
    $set: {
        isPrinted: true
    }

consider there this special case where both are false when it needs to be changed too true. But I do wonder if there is a way to iterate over multiple document id's with ease.

Robolisk
  • 1,682
  • 5
  • 22
  • 49