Scala Operation MongoDB

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.