MongoDB Scala - 查询特定字段值的文档

时间:2018-02-28 05:04:55

标签: mongodb scala mongo-java-driver

所以我知道在Mongo Shell中,你使用点符号来获取任何文档中你想要的字段。

如何在MongoDB Scala中实现点符号。我对它是如何工作感到困惑。以下是从集合中提取文档的代码:

def getLatestCommitOffsetFromDB(collectionName: String): Long = {

import com.mongodb.Block
import org.bson.Document

val printBlock = new Block[Document]() {
  override def apply(document: Document): Unit = {
    println(document.toJson)
  }
}

import com.mongodb.async.SingleResultCallback
val callbackWhenFinished = new SingleResultCallback[Void]() {
  override def onResult(result: Void, t: Throwable): Unit = {
    System.out.println("Latest offset fetched from database.")
  }
}

var obj: String = " "

try {

  val record = collection.find().projection(fields(include("offset"), excludeId())).limit(1)
  //TODO FIND A WAY TO GET THE VALUE AND STORE IT IN A VARIABLE

} catch {
  case e: RuntimeException =>
    logger.error(s"MongoDB Server Error : Unable to fetch data from collection : $collection")
    logger.error(e.printStackTrace().toString())
}

obj.toLong

}

编辑:

我正在努力研究一种机制,以便在消费者关闭时基本上重新使用Kafka记录。为此,我将我的kafka记录存储在外部数据库中,然后尝试从那里获取最近的偏移并从该点开始消耗。这是我应该这样做的Scala方法:

<?xml version="1.0" encoding="UTF-8"?>

问题不在于我可以从Mongo中获取文档,因此我正在尝试访问Mongo中的特定字段。文档中有四个字段:主题,分区,消息和偏移量。我想获取“offset”字段并将其存储在变量中,因此我可以将其用作重新启动点来重新使用Kafka记录。

我从哪里去?

的pom.xml

<groupId>OffsetManagementPoC</groupId>
<artifactId>OffsetManagementPoC</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.12</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-compiler</artifactId>
        <version>2.11.8</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-streams</artifactId>
        <version>0.10.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-streaming_2.11</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
        <version>2.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-10 -->
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
        <version>2.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.5</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>casbah_2.12</artifactId>
        <version>3.1.1</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.typesafe</groupId>
        <artifactId>config</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb.scala</groupId>
        <artifactId>mongo-scala-driver_2.12</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-compiler</artifactId>
        <version>2.11.8</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb.scala</groupId>
        <artifactId>mongo-scala-driver_2.11</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>bson</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-async</artifactId>
        <version>3.4.3</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb.scala</groupId>
        <artifactId>mongo-scala-bson_2.11</artifactId>
        <version>2.1.0</version>
    </dependency>
</dependencies>

http://maven.apache.org/xsd/maven-4.0.0.xsd“&GT;     4.0.0

    <div id="des"></div>
<div id="calcul" class="mb-20 mt-20">
    <select name="postchoose" id="" onchange="addInputHidden(this)">
        <option class="form-control" value="">Select Shiping Method</option>

    </select>
    <div id="courierinfo">
        <!--i want my hidden input in this div-->

    </div>  
</div>
<script >
    function addInputHidden(element) {
        var $option = $(element).find('option:selected'),
                code = $option.data('code'),
                service = $option.data('service');
        if (!jQuery('#courierinfo .code_' + code).length) {
            jQuery('#courierinfo').append('<input class="code code_' + code + '" name="code[]" type="hidden" value="' + code + '"><input class="service code_' + code + '" name="service[]" type="hidden" value="' + service + '">');
        } else {
            jQuery('#courierinfo .code.code_' + code).val(code)
            jQuery('#courierinfo .service.code_' + code).val(service)
        }
    }

    jQuery(document).ready(function ($) {
        $('body').on('change', 'select[name="city"]', function (e) {
            $.ajaxSetup({
                headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
            });
            var cityID = $(this).val();
            var weight = ["{{$totalWeight}}"];
            if (cityID) {
                $.ajax({
                    url: '{{ url('rajaajax') }}/' + weight + '/' + encodeURI(cityID),
                    type: "GET",
                    dataType: "json",
                    success: function (data) {
                        $('#des').empty();
                        $('#des').append(
                                '<p>Destination: ' + data['meta']['destination']['province'] + ' , ' + data['meta']['destination']['type'] + ' , ' + data['meta']['destination']['city_name'] + ' , ' + data['meta']['destination']['postal_code'] + '</p>'
                                );
                        $.each(data.data, function (key, value) {
                            $.each(value.costs, function (key2, value2) {
                                $.each(value2.cost, function (key3, value3) {

                                    // number format
                                    var number = value3['value'];
                                    var nf = new Intl.NumberFormat('en-US', {
                                        maximumFractionDigits: 0,
                                        minimumFractionDigits: 0
                                    });
                                    var formattedNumber = nf.format(number);
                                    // number format
                                    $('select[name="postchoose"]').append('<option id="postchoose" class="form-control" value="' + value3['value'] + '" data-code="' + value['code'] + '" data-code="' + value2['service'] + '">' + value['code'] + ' - ' + value2['service'] + ' - ' + nf.format(number) + ' Rp' + ' - ' + value3['etd'] + '</option>');
                                    // console.log(value);
                                    // alert(value.code); // jne-pos
                                    // alert(value2.service); //oke -reg
                                    // alert(value3.value); // 43000 - etd 24 jam
                                });
                            });
                        });
                    }
                });
            } else {
                $('select[name="postchoose"]').empty().append("<option value='' selected>Select</option>");
            }
        });
    });

</script>

$('select[name="postchoose"]').on('change',function(){
  var $option = $(this).find('option:selected),
  code = $option.data('code'),
  service = $option.data('service');
  // you can use ajax to send 
})

2 个答案:

答案 0 :(得分:1)

您可以这样修改查询:

classHour

我认为您错过了import com.mongodb.MongoClient import com.mongodb.client.MongoCollection import com.mongodb.client.model.Projections def getLatestCommitOffsetFromDB( databaseName: String, collectionName: String ): Long = { val mongoClient = new MongoClient("localhost", 27017); val collection = mongoClient.getDatabase(databaseName).getCollection(collectionName) val record = collection .find() .projection( Projections .fields(Projections.include("offset"), Projections.excludeId())) .first record.get("offset").asInstanceOf[Double].toLong } 次导入,以便使用com.mongodb.client.model.Projectionsfieldsinclude

我使用excludeId代替first,以便更轻松地提取结果。

limit(1)会返回一个first对象,您可以在该对象上调用Document来检索所请求字段的值。

但事实上,由于你只想要一个记录和一个字段,你可以删除投影!:

get

答案 1 :(得分:0)

根据文档,collection.find()接受goalsFor=

您可以使用的该接口的一个实现是BasicDBObject,它基本上类似于,。您可以使用接受地图的构造函数:

com.mongodb.DBObject