akira@perc01:/data$ #OK, let's get this party started!
akira@perc01:/data$ # The frontend has been shut down for 20 mins so they can
akira@perc01:/data$ # update that part, and I can update the schema in he
akira@perc01:/data$ # backend simultaneously.
akira@perc01:/data$ #Easy-peasy ...
akira@perc01:/data$ date
Tue Jul 2 13:34:09 JST 2019
akira@perc01:/data$ #Just set my auth details.(NO PEEKING!)
akira@perc01:/data$ conn_args="--host localhost:27017 --username akira --password secret --authenticationDatabase admin"
akira@perc01:/data$ mongo ${conn_args} --quiet
testrs:PRIMARY> use payments
switched to db payments
testrs:PRIMARY> show collections
TheImportantCollection
testrs:PRIMARY> //Ah, there it is. Time to work!
testrs:PRIMARY> db.TheImportantCollection.count()
174662
testrs:PRIMARY> db.TheImportantCollection.findOne()
{
"_id" : 0,
"customer" : {
"fn" : "Smith",
"gn" : "Ken",
"city" : "Georgevill",
"street1" : "1 Wishful St.",
"postcode" : "45031"
},
"order_ids" : [ ]
}
testrs:PRIMARY> //Ah, there it is. The "customer" object that has the
testrs:PRIMARY> //address fields in it. We're going to move those out.
testrs:PRIMARY> //Copy the whole collection, adding the new "addresses" array
testrs:PRIMARY> var counter = 0;
testrs:PRIMARY> db.TheImportantCollection.find().forEach(function(d) {
... d["adresses"] = [ ];
... db.TheImportantCollectionV2.insert(d);
... counter += 1;
... if (counter % 25000 == 0) { print(counter + " updates done"); }
... });
25000 updates done
50000 updates done
75000 updates done
100000 updates done
125000 updates done
150000 updates done
testrs:PRIMARY> //Cool. Let's look at the temp table
testrs:PRIMARY> db.TheImportantCollectionV2.findOne()
{
"_id" : 0,
"customer" : {
"fn" : "Smith",
"gn" : "Ken",
"city" : "Georgevill",
"street1" : "1 Wishful St.",
"postcode" : "45031"
},
"order_ids" : [ ],
"adresses" : [ ]
}
testrs:PRIMARY> //?AH!!
testrs:PRIMARY> //typo. I misspelled "addresses".
testrs:PRIMARY> //I'll just drop this and go again
testrs:PRIMARY> db.TheImportantCollectionV2.remove({})
WriteResult({ "nRemoved" : 174662 })
testrs:PRIMARY> //ooops. Why did I bother deleting the docs?
testrs:PRIMARY> //I need to *drop* the collection
testrs:PRIMARY> db.TheImportantCollection.drop()
true
testrs:PRIMARY> //!!!!
testrs:PRIMARY> //Wait!
testrs:PRIMARY> show collections
TheImportantCollectionV2
testrs:PRIMARY> //...
testrs:PRIMARY> //I've done a bad thing ....
testrs:PRIMARY> //Let me see
testrs:PRIMARY> //in the oplog
testrs:PRIMARY> use local
switched to db local
testrs:PRIMARY> db.oplog.rs.findOne({"o.drop": "TheImportantCollection"})
{
"ts" : Timestamp(1562042272, 1),
"t" : NumberLong(6),
"h" : NumberLong("6726633412398410781"),
"v" : 2,
"op" : "c",
"ns" : "payments.$cmd",
"ui" : UUID("abc9c1f9-71c0-45ea-aeba-ea239b975a95"),
"wall" : ISODate("2019-07-02T04:37:52.171Z"),
"o" : {
"drop" : "TheImportantCollection"
}
}
testrs:PRIMARY> //AH. 1562042272, you are the worst unix epoch second of my
testrs:PRIMARY> // life.
testrs:PRIMARY>