官术网_书友最值得收藏!

Updating and deleting data from the shell

This again will be a simple recipe that will be looking at executing deletes and updates on a test collection. We won't be dealing with the same test data that we imported as we don't want to update/delete any of that, but instead, we will work on a test collection created for this recipe only.

Getting ready

For this recipe, we will create a collection called updAndDelTest. We will require the server to be up and running. Refer to the Installing single node MongoDB recipe from Chapter 1, Installing and Starting the Server for instructions on how to start the server. Start the shell with the UpdAndDelTest.js script loaded. This script will be available on the Packt website for download. To know how to start the shell with a script preloaded, refer to the Connecting to a single node in the Mongo shell with JavaScript recipe in Chapter 1, Installing and Starting the Server.

How to do it…

  1. Start the MongoDB shell and preload the script:
    $ mongo --shell updAndDelTest.js
    
  2. With the shell started and script loaded, execute the following in the shell:
    > prepareTestData()
    
  3. If all goes well, you should see Inserted 20 documents in updAndDelTest printed to the console:
  4. To get a feel of the collection, let's query it as follows:
    > db.updAndDelTest.find({}, {_id:0})
    
  5. We should see that for each value of x as 1 and 2, we have y incrementing from 1 to 10 for each value of x.
  6. We will first update some documents and observe the results. Execute the following update:
    > db.updAndDelTest.update({x:1}, {$set:{y:0}})
    
  7. Execute the following find command and observe the results; we should get 10 documents. For each of them, note the value of y.
    > db.updAndDelTest.find({x:1}, {_id:0})
    
  8. We shall now execute the following update:
    > db.updAndDelTest.update({x:1}, {$set:{y:0}}, {multi:true})
    
  9. Executing the query given in step 6 again to view the updated documents. It will show the same documents that we saw earlier. Take a note of the values of y again and compare them to the results that we saw when we executed this query last time before executing the update given in step 7.
  10. We will now see how delete works. We will again choose the documents where x is 1 for the deletion test. Let's delete all the documents where x is 1 from the collection:
    > db.updAndDelTest.remove({x:1})
    
  11. Execute the following find command and observe the results. We will not get any results. It seems that the remove operation has removed all the documents with x as 1.
    > db.updAndDelTest.find({x:1}, {_id:0})
    

    Note

    When you are in the mongo shell and you want to see the source code of a function, simply type in the function name without the parenthesis. For example, in this recipe, we can view the code of our custom function by typing the function name, prepareTestData, without the parenthesis, and press Enter.

How it works…

First, we set up the data that we will use for the updating and deleting test. We have already seen the data and know what it is. An interesting thing to observe is that when we execute an update such as db.updAndDelTest.update({x:1}, {$set:{y:0}}), it only updates the first document that matches the query provided as the first parameter. This is something we will observe when we query the collection after this update. The update function has the following format db.<collection name>.update(query, update object, {upsert: <boolean>, multi:<boolean>}).

We will see what upsert is in the later recipes. The multi parameter is set to false by default. This means that multiple documents will not be updated by the update method; only the first matching document will be updated. However, when we do db.updAndDelTest.update({x:1}, {$set:{y:0}}, {multi:true}) with multi set to true, all the documents in the collection that match the given query are updated. This is something that we can verify after querying the collection.

Removals, on the other hand, behave differently. By default, the remove operation deletes all the documents that match the provided query. However, if we want to delete only one document, we explicitly pass the second parameter as true.

Note

The default behavior of update and remove is different. An update call, by default, updates only the first matching document, whereas remove deletes all the documents matching the query.

主站蜘蛛池模板: 阿坝| 双辽市| 游戏| 上虞市| 大同市| 珠海市| 锡林浩特市| 天门市| 杂多县| 库车县| 皮山县| 盱眙县| 平湖市| 康平县| 屯留县| 成武县| 克拉玛依市| 塔河县| 大连市| 延安市| 乐业县| 泾源县| 呼和浩特市| 茌平县| 铜梁县| 桃园县| 龙陵县| 托里县| 鄢陵县| SHOW| 衡阳县| 千阳县| 吐鲁番市| 汤原县| 京山县| 醴陵市| 高州市| 盘山县| 涞水县| 安西县| 牟定县|