File: update.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (28 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true
# rubocop:todo all

# Update top-level fields in a single document

client[:restaurants].find(name: 'Juni').update_one('$set'=> { 'cuisine' => 'American (New)' },
                                                   '$currentDate' => { 'lastModified'  => true })

# Update an embedded document in a single document

client[:restaurants].find(restaurant_id: '41156888').update_one('$set'=> { 'address.street' => 'East 31st Street' })

# Update multiple documents

client[:restaurants].find('address.zipcode' => '10016').update_many('$set'=> { 'borough' => 'Manhattan' },
                                                                    '$currentDate' => { 'lastModified'  => true })

# Replace the contents of a single document

client[:restaurants].find(restaurant_id: '41704620').replace_one(
                                                       'name' => 'Vella 2',
                                                       'address' => {
                                                          'coord' => [-73.9557413, 40.7720266],
                                                          'building' => '1480',
                                                          'street' => '2 Avenue',
                                                          'zipcode' => '10075'
                                                        }
                                                     )