Scala Operation MongoDB
Maven
<Dependencies> <Dependency> <groupId>Org.mongodb</groupId> <Artifactid>Casbah</Artifactid> <version>3.1.1</version> </Dependency></Dependencies>
Sbt
Librarydependencies + = "Org.mongodb" percent "Casbah"% "3.1.1"
Lib
Import Com.mongodb.casbah.imports._
CONNECTION
Val mongoclient= mongoclient ("localhost", 27017= Mongoclienturi (" mongodb://localhost:27017/ " = mongoclient (URI)
Use COLLECTION
Val db = Mongoclient ("test"= db ("ABC")
INSERT
Val user1 = Mongodbobject ("name"-"User1","Email"-"[email protected]") Val User2= Mongodbobject ("name"-"User2","Email"-"[email protected]") Val User3= Mongodbobject ("name"-"User3","Email"-"[email protected]") Coll.insert (user1) Coll.insert (user2) Coll.insert (User3)
SELECT
Coll.count ()//=mongodbobject ("name""user1") Coll.findone (Query1)
UPDATE
Val update1 = Mongodbobject ("email""[email protected]" = Coll.update (Query1, update1)
Upsert=True if the Insert//Val result = coll.update (query, update, upsert=true) is not present
// = mongodbobject (" name -- User2 , " Email , " [email protected] ) Val Update3 = $set ( email "-" [email protected] ) val Result3 = coll.update ( Query3, Update3, multi=true)
DELETE
Val Query2 = Mongodbobject ("name""user2"= Coll.remove (Query2)
Scala Operation MongoDB