Archive for October, 2018
MongoDB Update Statement
While discussing the pros and cons of MongoDB, my students wanted to know how to update a specific element in a collection. Collections are like tables in relational databases.
You create the users
collection by inserting rows like this:
db.users.insert( [ { contact_account: "CA_20170321_0001" , first_name: "Jonathan" , middle_name: "Eoin" , last_name: "MacGregor" , addresses: { street_address: ["1111 Broadway","Suite 101"] , city: "Oakland" , state: "CA" , zip: "94607" } } , { contact_account: "CA_20170328_0001" , first_name: "Remington" , middle_name: "Alain" , last_name: "Dennison" , addresses: { street_address: ["2222 Broadway","Suite 121"] , city: "Oakland" , state: "CA" , zip: "94607" } } ]) |
You can query the results with the db.users.find({})
command, or you can query the formatted results with the following command:
db.users.find({}).pretty() |
You can provide a simple update of middle_name
element of a given collection element with the findAndModify()
function. The following queries the users
collection to find the JSON middle_name
element where the contact_account
value is equal to the “CA_20170330_0001
” string.
db.users.findAndModify( { query: { contact_account: "CA_20170328_0001" } , update: { $set: { middle_name: "Alan" }} , upsert: false }) |
After changing the middle_name value from “Alain” to “Alan”, you can query the single element of the collection with the following:
db.users.find({ contact_account: "CA_20170328_0001" }).pretty() |
It should return the following:
{ "_id" : ObjectId("5bd7f69ba135dda917665de7"), "contact_account" : "CA_20170328_0001", "first_name" : "Remington", "middle_name" : "Alan", "last_name" : "Dennison", "addresses" : { "street_address" : [ "2222 Broadway", "Suite 121" ], "city" : "Oakland", "state" : "CA", "zip" : "94607" } } |
You can replace the addresses
string element value a collection of elements with the following findAndModify()
function:
db.users.findAndModify( { query: { contact_account: "CA_20170328_0001" } , update: { $set: { addresses: [ { active_status: true , start_date : new Date("2018-10-30") , street_address: ["2222 Broadway","Suite 121"] , city: "Oakland" , state: "CA" , zip: "94607" } , { active_status: false , start_date: new Date("2017-10-01") , end_date : new Date("2018-10-29") , street_address: ["2222 Broadway","Suite 121"] , city: "Oakland" , state: "CA" , zip: "94607" } ] } } , upsert: false }) |
You can re-query the modified result set with find()
function with the same query syntax as used previously. This looks for a specific member element in the collection by matching the contact_account
name’s value pair. It is the same as the one used earlier in this blog post.
db.users.find({ contact_account: "CA_20170328_0001" }).pretty() |
It should return the following:
{ "_id" : ObjectId("5bd7f69ba135dda917665de7"), "contact_account" : "CA_20170328_0001", "first_name" : "Remington", "middle_name" : "Alan", "last_name" : "Dennison", "addresses" : [ { "active_status" : true, "start_date" : ISODate("2018-10-30T00:00:00Z"), "street_address" : [ "2222 Broadway", "Suite 121" ], "city" : "Oakland", "state" : "CA", "zip" : "94607" }, { "active_status" : false, "start_date" : ISODate("2017-10-01T00:00:00Z"), "end_date" : ISODate("2018-10-29T00:00:00Z"), "street_address" : [ "2222 Broadway", "Suite 121" ], "city" : "Oakland", "state" : "CA", "zip" : "94607" } ] } |
As always, I hope this helps someone.
Linux mongod Service
The installation of MongoDB doesn’t do everything for you. In fact, the first time you start the mongod
service, like this as the root
user or sudoer user with the command:
service mongod start |
A sudoer user will be prompted for their password, like
A typical MongoDB instance raises the following errors:
Redirecting to /bin/systemctl start mongod.service [student@localhost cit425]$ mongo MongoDB shell version v3.4.11 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.11 Server has startup warnings: 2018-10-29T10:51:57.515-0600 I STORAGE [initandlisten] 2018-10-29T10:51:57.515-0600 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2018-10-29T10:51:57.515-0600 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2018-10-29T10:51:58.264-0600 I CONTROL [initandlisten] 2018-10-29T10:51:58.264-0600 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-10-29T10:51:58.264-0600 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-10-29T10:51:58.264-0600 I CONTROL [initandlisten] 2018-10-29T10:51:58.265-0600 I CONTROL [initandlisten] 2018-10-29T10:51:58.265-0600 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 15580 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files. |
You can fix this by following the MongoDB instructions for the Unix ulimit Settings, which will tell you to create a mongod
file in the /etc/systemd/system
directory. You should create this file as the root
superuser. This is what you should put in the file:
[Unit] Description=MongoDB Documentation=man:mongo [Service] # Other directives omitted # (file size) LimitFSIZE=infinity # (cpu time) LimitCPU=infinity # (virtual memory size) LimitAS=infinity # (locked-in-memory size) LimitMEMLOCK=infinity # (open files) LimitNOFILE=64000 # (processes/threads) LimitNPROC=64000 |
Then, you should be able to restart the mongod service without any warnings with this command:
service mongod restart |
As always, I hope this helps somebody.
Fedora 27 Screen Saver
Just back from Oracle OpenWorld and somebody desperately wants to know how to disable the 5 minute default screen saver setting in the KDE environment. OK, you navigate to the Fedora “f” in the lower left hand corner and choose System Settings, like:
In the System Setting menu page, select Desktop Behavior:
In the Desktop Behavior dialog, select Screen Locking. Change the default to something large like 90 for 1 and half hours.
As always, I hope that solves your problem.