Introduced
Scala is a kind of functional object-oriented language . It blends a lot of unprecedented features. At the same time, it executes on top of the JVM . With the growing interest of developers in Scala , and the increasing number of tools to support it, the Scala language will undoubtedly be an indispensable tool in your hands.
MongoDB is a product between a relational database and a non-relational database, which is the most versatile and most like a relational database. The data structure he supports is very loose and is a json - like bson format. It is therefore possible to store more complex data types.
Mongo The biggest feature is that the query language he supports is very powerful, and its syntax is a bit like an object-oriented query language, almost capable of implementing most of the functions of a relational database single-table query, and also supports indexing of data .
in this article. We'll show you how to connect to Mongodb and data processing using the Scala language .
Environment Installation and configuration
First. Install MongoDB and Scala, assuming it's already installed. Be able to skip this step. Detailed installation procedures please search by yourself.
Second. Install SBT. SBT is the build tool in Scala. function equivalent to maven.
Installation is relatively simple, detailed steps please refer to: http://www.scala-sbt.org/0.13/tutorial/zh-cn/Setup.html
Third, after installation is complete. Running the SBT command on the console, a message similar to the following appears:
[Email protected] scala-project]# SBT
[INFO] Loading Global Plugins From/root/.sbt/0.13/plugins
[INFO] Set current project to Casbah Tutorial (in buildfile:/home/apache/src/scala-project/)
>
Four: Create a test item and load it into the Casbah library. Casbah is MongoDB's Scala driver
mkdir Test-project
CD Test-project
VI BUILD.SBT, Input:
Name: = "Casbah Test"
Version: = "1.0"
Scalaversion: = "2.11.2"
Librarydependencies + = "Org.mongodb" percent "Casbah"% "2.7.3"
Note: There is a blank line cut for each line in the BUILD.SBT file.
If you have any questions, please refer to http://mongodb.github.io/casbah/guide/installation.html
After completion of the input command: Sbtconsole, the first time the implementation will voluntarily download the Casbah and other dependent libraries, after loading for example, such as the following information is possible.
Welcome to Scala version 2.11.2 (Java HotSpot (TM) 64-bit Server Vm,java 1.7.0_13).
Type in expressions to has them evaluated.
Type:help for more information.
Scala>
Connect to MongoDB
Loading Scala SHELL:SBT Console
Importcom.mongodb.casbah.imports._
Val mongoclient= mongoclient ("localhost", 27017)
Note: You can enter mongoclient in the console. + SPACEBAR, the system will proactively prompt for available actions.
Multiple connection modes are supported:
1. Simple Way
Connect to Default-localhost, 27017
Val mongoclient = Mongoclient ()
Connect to "MONGODB01" host, default port
Val mongoclient = mongoclient ("Mongodb01")
Connect to "MONGODB02" host, Port 42017
Val mongoclient = mongoclient ("mongodb02", 42017)
2. Uri method
Val uri = Mongoclienturi ("mongodb://localhost:27017/")
Val mongoclient = mongoclient (URI)
3. Support Authorization method
Challenge Response
Valserver = new ServerAddress ("localhost", 27017)
Valcredentials = Mongocredential.createmongocrcredential (UserName, Database,password)
Valmongoclient = mongoclient (server, List (credentials))
X.509protocol
Valserver = new ServerAddress ("localhost", 27017)
Valcredentials = mongocredential.createmongox509credential (userName)
Valmongoclient = mongoclient (server, List (credentials))
...
Many other connection information, please refer to: http://mongodb.github.io/casbah/guide/connecting.html
We create a test_db database and test collection in MongoDB, and choose a simple user password way, such as the following:
Val Server = newserveraddress ("localhost", 27017)
Val Credentials =mongocredential.createmongocrcredential ("User", "test_db", "Pass". ToArray)
Val mongoclient= mongoclient (server, List (credentials))
A connection that appears similar to the following information is successful:
res2:com.mongodb.casbah.MongoClient = [email protected]
Get databases and connections:
Val db = Mongoclient ("test_db")
Db.collectionnames
Val coll = db ("Test")
Adding and removing changes and checking operation
Add to
Val user1 = mongodbobject ("name", "user1")
Val user2 = mongodbobject ("name", "User2")
Coll.insert (User1)
Coll.insert (User2)
Read
Coll.count ()//Read total number of records
You can also use Find to read records, such as reading all records and printing them:
Coll.find (). foreach (println)
Val query1 =mongodbobject ("name", "user1")
Coll.findone (Query1)
Update
Val update1 =mongodbobject ("email", "[email protected]")
Val result1 = coll.update (query1,update1)
println ("number updated:" +RESULT.GETN)
or Val result = coll.update (query, update, upsert=true)//upsert=true, assuming there is no insert
Delete
Val Query2 = mongodbobject ("name", "User2")
Val result2 = Coll.remove (Query2)
Delete Collection
Coll.drop ()
Many other use methods please refer to Casbah user documentation http://mongodb.github.io/casbah/index.html
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Use Scala to manipulate MongoDB