Scala对MongoDB的增删改查操作

依赖环境:jdk1.8、Scala 2.10、idea

===========================================

    转载原文链接: Scala对MongoDB的增删改查操作 转载请注明出处!

            http://www.cnblogs.com/PerkinsZhu/p/6917104.html

===========================================





maven 依赖

需要注意一下,在导入的jar包存在两个MongoClient类,

一个来自于mongo-java-driver.jar的com.mongodb.MongoClient    ====>用于java调用

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.7.1</version>
</dependency>

另一个来自于casbah-core_2.12:3.1.1.jar的com.mongodb.casbah.MongoClient.   ==  Scala使用后者!!!

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>casbah_2.10</artifactId>
    <version>3.1.1</version>
    <type>pom</type>
</dependency>


一、创建数据库连接

    MongoClient

A:不需要用户名和密码直接获取MongoDB。

//  无权限验证连接
  def createDatabase(url: String, port: Int, dbName: String): MongoDB = {
    MongoClient(url, port).getDB(dbName)
  }

B:通过权限验证进行连接

 //验证连接权限
  def createDatabase(url: String, port: Int, dbName: String, loginName: String, password: String): MongoDB = {
    var server = new ServerAddress(url, port)
    //注意:MongoCredential中有6种创建连接方式,这里使用MONGODB_CR机制进行连接。如果选择错误则会发生权限验证失败
    var credentials = MongoCredential.createCredential(loginName, dbName, password.toCharArray)
    var mongoClient = MongoClient(server, List(credentials))
    mongoClient.getDB(dbName)
  }

这里需要注意的是MongoCredential.createCredential(),

MongoCredential中存在着六种认证机制,这里使用createCredential()进行创建,使用错误则将会验证失败

com.mongodb.MongoSecurityException: Exception authenticating

Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. 

        The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18, "codeName" : "AuthenticationFailed" }

/**
 * Creates a MongoCredential instance with an unspecified mechanism.  The client will negotiate the best mechanism
 * based on the version of the server that the client is authenticating to.  If the server version is 2.8 or higher,
 * the driver will authenticate using the SCRAM-SHA-1 mechanism.  Otherwise, the driver will authenticate using the
 * MONGODB_CR mechanism.
 *
 * @param userName the user name
 * @param database the database where the user is defined
 * @param password the user's password
 */

二、数据添加

在进行数据插入的时候,如果不存在该collection则会自动创建(这里是user),如果document中不包含“_id”字段则会自动添加该字段。

DBCollection中存在一个save方法,该save方法区别于insert的地方在于当“_id”存在于集合中,save将会进行更新数据,而insert不会进行任何操作

 根据需要选择适合的数据插入函数

存储之后数据如下:

 { "_id" : { "$oid" : "592ad4be45aefd09f4867f1e"} , "name" : "Jack86" , "email" : "[email protected]" , "age" : 11 , "birthDay" : { "$date" : "2016-03-24T16:00:00.000Z"}}
 { "_id" : { "$oid" : "592ad4be45aefd09f4867f1f"} , "name" : "Jack87" , "email" : "[email protected]" , "age" : 12 , "birthDay" : { "$date" : "2016-03-24T16:00:00.000Z"}}
{ "_id" : { "$oid" : "592ad4be45aefd09f4867f20"} , "name" : "Jack88" , "email" : "[email protected]" , "age" : 13 , "birthDay" : { "$date" : "2016-03-24T16:00:00.000Z"}}

{ "_id" : { "$oid" : "592ad4be45aefd09f4867f21"} , "name" : "Jack89" , "email" : "[email protected]" , "age" : 14 , "birthDay" : { "$date" : "2016-03-24T16:00:00.000Z"}}

三、数据更新修改

A:更新方式一

复制代码
  def testUpdate(): Unit = {
    var query = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    var value = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    println("=========更新之前============")
    var query02 = MongoDBObject("name" -> "user1")
    collection.find(query02).forEach(x => println(x))
//    query:根据此条件进行查询  value:把查询出来结果集的第一条数据设置为value
    collection.update(query,value)
    println("=========更新之后============")
    collection.find(query02).forEach(x => println(x))
  }
复制代码

 

运行结果如下:(注意这里只更新了一条记录,并不是把所有符合结果的数据都进行更新

B:更新方式二

复制代码
  def testUpdate02(): Unit = {
    var query = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    var value = new BasicDBObject("$set", new BasicDBObject("email", "[email protected]"))
    //     var value = MongoDBObject("$set",MongoDBObject("name" -> "user1", "email" -> "[email protected]"))
    println("=========更新之前============")
    var query02 = MongoDBObject("name" -> "user1")
    collection.find(query02).forEach(x => println(x))
    collection.update(query, value,true, true)
    println("=========更新之后============")
    collection.find(query02).forEach(x => println(x))
  }
复制代码

 

 注意该方法:collection.update(query, value,true, true)。

    第三个参数:when true, inserts a document if no document matches the update query criteria

    第四个参数:when true, updates all documents in the collection that match the update query criteria, otherwise only updates one【当该值为true时,则更新所有的结果集数据,不过前提是value必须使用“$XXX”模式进行定义】。源码如下:

 

四、数据查询(建议看下这篇文章:MongoDB学习笔记(查询)

复制代码
  def testSelect(): Unit ={
    println("=========查询所有数据===================")
    collection.find().forEach(x => println(x))
    println("=========查询name = “user1”  同时email=“[email protected]”===================")
    collection.find(MongoDBObject("name" -> "user1", "email" -> "[email protected]")).limit(3).forEach(x => println(x))
    //    注意此处不能使用put添加其他查询条件,因为put返回的是HashMap,此处应该使用append进行添加查询条件
    //  var query = new BasicDBObject("name",new BasicDBObject("$in",("user145","user155"))).put("qty",new BasicDBObject("$in",(25.0,105.0)))  该方法错误
    //    查询条件为: (name in ("user145","user155")) && (qty in (25.0,105.0))
    println("=========查询 (name in (\"user145\",\"user155\")) && (qty in (25.0,105.0))===================")
    var query = new BasicDBObject("name", new BasicDBObject("$in", ("user145", "user155"))).append("qty", new BasicDBObject("$in", (25.0, 105.0)))
    collection.find(query).forEach(x => println(x))
    println("=========查询 start >= 10 && end<= 80 的数据===================")
    var query02 = new BasicDBObject("start", new BasicDBObject("$gte", 10)).append("end", new BasicDBObject("$lte", 80))
    collection.find(query02).forEach(x => println(x))
  }
复制代码

 

 查询结果如下:

五、数据删除

数据删除主要是构建一个查询条件,把符合该条件的所有数据都进行删除

复制代码
  def testDelete(): Unit ={
    var query = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    println("=========删除之前============")
    collection.find(query).forEach(x => println(x))
    //该参数只是一个查询条件,符合该条件的所有集合都将被删除。重点在于如何构建query
    collection.remove(query)
    println("=========删除之前============")
    collection.find(query).forEach(x => println(x))
  }
复制代码

  执行结果如下:

=========删除之前============

{ "_id" : { "$oid" : "592a0c8345aefd1d404c3ed9"} , "name" : "user1" , "email" : "[email protected]"}
{ "_id" : { "$oid" : "592a0c8345aefd1d404c3eda"} , "name" : "user1" , "email" : "[email protected]"}
{ "_id" : { "$oid" : "592a0c9845aefd0dfc2a1e21"} , "name" : "user1" , "email" : "[email protected]"}
{ "_id" : { "$oid" : "592a0c9845aefd0dfc2a1e23"} , "name" : "user1" , "email" : "[email protected]"}
{ "_id" : { "$oid" : "592a740f434a43d3b1529d0e"} , "name" : "user1" , "email" : "[email protected]"}
=========删除之后============

remove方法有如下四种,根据具体情况选取合适的方法:

 
 

附测试源码:

复制代码
package tool

import java.text.SimpleDateFormat
import java.util
import java.util.Date

import com.mongodb.casbah.commons.MongoDBObject
import com.mongodb.{BasicDBObject, DBCollection, ServerAddress}
import com.mongodb.casbah.{MongoClient, MongoCredential, MongoDB}
import com.mongodb.client.model.Filters
import org.joda.time.DateTime

/**
  * Created with IntelliJ IDEA.
  * Description:
  * User: Perkins Zhu
  * Date: 2017-05-28
  * Time: 19:33
  */
object MongoTool {
  def main(args: Array[String]): Unit = {
//        testInsert
//    testUpdate02
//    testSelect
  testDelete
  }
  def testDelete(): Unit ={
    var query = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    println("=========删除之前============")
    collection.find(query).forEach(x => println(x))
    //该参数只是一个查询条件,符合该条件的所有集合都将被删除
    collection.remove(query)
    collection.findAndRemove()
    println("=========删除之前============")
    collection.find(query).forEach(x => println(x))
  }


  def testUpdate01(): Unit = {
    var query = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    var value = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    println("=========更新之前============")
    var query02 = MongoDBObject("name" -> "user1")
    collection.find(query02).forEach(x => println(x))
//    query:根据此条件进行查询  value:把查询出来结果集的第一条数据设置为value
    collection.update(query,value)
    println("=========更新之后============")
    collection.find(query02).forEach(x => println(x))
  }

  def testUpdate02(): Unit = {
    var query = MongoDBObject("name" -> "user1", "email" -> "[email protected]")
    var value = new BasicDBObject("$set", new BasicDBObject("email", "[email protected]"))
    //     var value = MongoDBObject("$set",MongoDBObject("name" -> "user1", "email" -> "[email protected]"))
    println("=========更新之前============")
    var query02 = MongoDBObject("name" -> "user1")
    collection.find(query02).forEach(x => println(x))
    collection.update(query, value,true, true)
    println("=========更新之后============")
    collection.find(query02).forEach(x => println(x))
  }

  def testSelect(): Unit ={
    println("=========查询所有数据===================")
    collection.find().forEach(x => println(x))
    println("=========查询name = “user1”  同时email=“[email protected]”===================")
    collection.find(MongoDBObject("name" -> "user1", "email" -> "[email protected]")).limit(3).forEach(x => println(x))
    //    注意此处不能使用put添加其他查询条件,因为put返回的是HashMap,此处应该使用append进行添加查询条件
    //  var query = new BasicDBObject("name",new BasicDBObject("$in",("user145","user155"))).put("qty",new BasicDBObject("$in",(25.0,105.0)))  该方法错误
    //    查询条件为: (name in ("user145","user155")) && (qty in (25.0,105.0))
    println("=========查询 (name in (\"user145\",\"user155\")) && (qty in (25.0,105.0))===================")
    var query = new BasicDBObject("name", new BasicDBObject("$in", ("user145", "user155"))).append("qty", new BasicDBObject("$in", (25.0, 105.0)))
    collection.find(query).forEach(x => println(x))
    println("=========查询 start >= 10 && end<= 80 的数据===================")
    var query02 = new BasicDBObject("start", new BasicDBObject("$gte", 10)).append("end", new BasicDBObject("$lte", 80))
    collection.find(query02).forEach(x => println(x))
  }

  def testInsert(): Unit = {
    for (i <- 1 to 100)
//      注意与saved的区别
      collection.insert(MongoDBObject("name" -> "Jack%d".format(i), "email" -> "jack%[email protected]".format(i), "age" -> i % 25, "birthDay" -> new SimpleDateFormat("yyyy-MM-dd").parse("2016-03-25")))
  }

  var collection = createDatabase("localhost", 27017, "mytest", "user", "123456").getCollection("user")

  //验证连接权限
  def createDatabase(url: String, port: Int, dbName: String, loginName: String, password: String): MongoDB = {
    var server = new ServerAddress(url, port)
    //注意:MongoCredential中有6种创建连接方式,这里使用MONGODB_CR机制进行连接。如果选择错误则会发生权限验证失败
    var credentials = MongoCredential.createCredential(loginName, dbName, password.toCharArray)
    var mongoClient = MongoClient(server, List(credentials))
    mongoClient.getDB(dbName)
  }

  //  无权限验证连接
  def createDatabase(url: String, port: Int, dbName: String): MongoDB = {
    MongoClient(url, port).getDB(dbName)
  }
}
复制代码


1.建立连接

 
 
import com.mongodb.DBObject
import com.mongodb.casbah.commons.MongoDBObject
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.{MongoClient, MongoClientURI, MongoCollection}
import com.mongodb.casbah.commons.ValidBSONType.ObjectId
import org.bson.types.ObjectId
object MongoDBTest {
  def main(args: Array[String]): Unit = {

    //val mongoString = "mongodb://host:port/database.collection"
    val mongoString = "mongodb://10.14.80.1:1201/unicom.job"

    val mongoClientURI: MongoClientURI = MongoClientURI(mongoString)
    println(s"mongoClientURI.database:${mongoClientURI.database.get}")
    println(s"mongoClientURI.collection:${mongoClientURI.collection.get}")
    if (mongoClientURI.collection.isEmpty) {
      throw new Exception(" mongoString collection is empty")
    }

    val mongoClient: MongoClient = MongoClient(mongoClientURI)
    val collection: MongoCollection = mongoClient(mongoClientURI.database.get)(mongoClientURI.collection.get)
    collection.insert(MongoDBObject("a" -> "b"))
    collection.insert(MongoDBObject("a" -> "b"))
    collection.insert(MongoDBObject("a" -> "b"))


    collection.update(MongoDBObject("date" -> "201507"), $inc("execTimes" -> 1), upsert = true)

    for (i <- collection.find()) println(i.containsField("_123"))
    println(collection.findOne().get.containsField("_id"))
    val t = collection.findOne().getOrElse("_id", "默认值")
    println(t)
    println(collection.remove(MongoDBObject("_id" -> t)))

    collection.findOne(MongoDBObject("_id" -> new ObjectId("577fb126c00d0567997bb525"))) match {
      case Some(obj) => obj.getAs[DBObject]("a.b") match {
        case Some(x) => x.foreach(f => println(f._2))
      }
      case None => println("find nothing")
    }

    collection.findOne(MongoDBObject("status" -> "WAITING"), orderBy = MongoDBObject("power" -> -1)) match {
      case Some(x) => println(x)
      case None => println("None")
    }

    collection.findOne(MongoDBObject("_id" -> new ObjectId("5799ca22bee6f300016efc73"))) match {
      case Some(x) =>
        println(x.getAs[Double]("kafka.topicInfo.partition"))
        println(123)
        println(x.getAs[Int]("kafka.topicInfo.partition"))
        println(123)
        println(x.getAs[String]("kafka.topicInfo.partition"))
        println(123)
      case None => println("none")
    }

    mongoClient.close()

  }


}

 
 





    

猜你喜欢

转载自blog.csdn.net/lixgjob/article/details/80686423