Update method is responsible for updating and re-drawing the game objects. Phaser by default will aim to run at 60 frames per second – that means, it updates your game 60 times per second, as long as its state is active.
In the create method, we reference second image(i.e., image with ‘com’ reference name) with a variable this.logo We could set the anchor point of rotation using anchor.set() method. If set to 0, it rotates with images right top corner as it’s rotating point. If set to 0.5, it rotates with images center as the rotating point. If set to 1, it rotates with the images left bottom corner as its rotating point.
Update Method Update method is called 60 times per second – so in our code, the value of rotation property increments 0.01 x 60 times for every second. Update method keeps running until there is any logical termination or with the shutdown of the state.
If we leave the first argument of find() method empty, it matches with all the documents of the collection. Hence it fetched all the Company names from the documents.
Here we have 3 arguments for update() method. First one is intentionally left empty. In second argument, we specify the changes needed to the documents. In third argument we specify the option, multi: true – which tells mongo engine to match with all the documents in the collection.
Lets learn to use upsert option with update() method.
upsert basically inserts document into the collection, if there is no matching document found in the collection. If there is a match found, then the document simply gets updated – in which case upsert option will not have any effect on the selected document.
Observe the 3rd argument of update() method – upsert: true. In this case, since there is no matching document found inside the “names” collection, for “Company” Xiaomi, it inserts the document with all the fields and values specified in the update command. i.e., Company: Xiaomi, Product: Mi3, No: 3
We have documents which has values 1, 2 and 3 for the field “No”. But in above command the condition is to select documents which has “No” value above 3 – which results in 0 documents being selected. But since we have “upsert: true” as the third argument of update() method, the “field: value” – “Product: Glass” and “No: 4” gets inserted into the “names” collection. So we need to take proper care of our commands and conditions – so that unintentional insertion of data or document can be avoided.
In this case, there is a document inside the collection which has “No”: 4. So the upsert option will not have any effect on the selected data/document – it simply gets updated with the field values specified in the second argument of update() method. i.e., the document with “No”: 4 is selected and the “Company”: “Sony”, “Product”: “Smart Watch” gets added to the existing document.
We can update the array by simply using update() method. But here, we need to remember all the elements of the array as well as the new element to be inserted into the array. Thus, this method is somewhat tedious.
We could insert an element into the array by making use of $set operator. Here we need to know the position where the new element needs to be inserted. In mongoDB, array index starts from zero.
using $push operator we can insert an element to the right hand side of the array. Here we simply specify the key and the value/element to be inserted.
$addToSet operator adds specified element to the array, if its not already present in the array. If the element is already present in the array, then it doesn’t add it once again.