Scala: How to insert documents into MongoDB with insert, save, and +=

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 16.4, “How to insert documents into MongoDB with insert, save, and +=.”


You want to save documents to a MongoDB collection from a Scala application.


Use the insert, save, or += methods of the Casbah MongoCollection class.

Android: Generic methods to write/save Android preferences data alvin February 17, 2015 - 8:57am

I was working with Android Preferences recently, and after writing a few “save my preference” methods, I decided to take a few minutes to organize my code and put all of those Preference methods in a PreferenceUtils class. I made the methods static, and following functional programming principles, I made their output depend only on their input, so they require you to pass in a Context reference, along with your preference key and value.

A collection of Scala XML tutorials alvin February 20, 2014 - 9:02pm

The following links are a collection of Scala XML tutorials I've written. Most of them come from the Scala Cookbook, while the others were written before I wrote the Cookbook.

Without any further ado, here are the links:

Scala: Saving/writing XML to a file

Problem: You want to write XML data to a file in a Scala application, such as saving application data or configuration information to a file.


Use the method to write a Scala literal to a file. Given this XML literal:

Saving Mac Stickies, the StickiesDatabase file, and syncing

Mac Stickies FAQ: Can I save Mac Stickies? If so, how? Also, is there any way to sync Mac Stickies between multiple computers?

This Mac Stickies question goes from simple to bizarre faster than any Mac FAQ I've ever been asked. I'll walk you through what I just learned, so please bear with me, answers are in here.

A CakePHP SQL UPDATE example alvin January 23, 2011 - 7:20pm

CakePHP SQL UPDATE FAQ: How do I perform a SQL UPDATE with CakePHP?

As I was writing a user registration process for a CakePHP application, I just found myself in a situation where I needed to write a CakePHP SQL UPDATE query. While you don't pay attention to it, you often run a SQL UPDATE query in CakePHP in a typical controller edit() function, but this was the first time I really had to do this manually.

The short answer is that you execute a SQL UPDATE query in CakePHP using the usual model save() method. The way the save method work is like this:

Java “file write” (or “file save”) methods alvin February 1, 2010 - 8:27am

Java write/save FAQ: Can you share an example of how to write to a file in Java?

Sure. Here are two "Java file save" or "Java file write" examples, taken from my Java file utilities article (Java file utilities to write, save, open, read, and copy files). I'm not going to explain these too much, but if you need a simple method to write to a file in your Java programs, I hope these methods will help you.

vi quit and exit tutorial

vim quit/save/exit FAQ: How do I quit/exit vim?

Answer: This depends by what you mean by the word exit. Here's a short list of the different ways I normally quit or exit a vi/vim editor session.

vi exit - no changes made to your file (vim quit command)

If you haven't made any changes to your file you can just quit your vi (or vim) editing session like this:

How to save the output from a MySQL query to a file

I had a problem with this website last Wednesday, and as part of troubleshooting the problem, I need to look at one of the Drupal database tables, specifically the watchdog table. This table had thousands of records in it, and I couldn't find what I needed with SQL SELECT queries, so I finally dug through the MySQL documentation, and found that I could save the output from a SELECT query to a text file.

In this tutorial I'll show you both (a) how to save the results of a MySQL query to a text file, and also (b) how to log your entire MySQL session to a text file.