简体   繁体   中英

How to accumulate aggregation results into a collection using MongoDB Scala Driver

I am migrating a code that I myself made in Java to Scala which does an aggregation in MongoDB. But I was stuck on how to accumulate the results of aggregation inside a collection using MongoDB Scala Driver.

Java code:

mongoCollection.aggregate(aggregatePipeline)
    .map(document -> {
      Document group = document.get("_id", Document.class);
      return new Document("chat", group).append("count", document.get("count"));
    })
    .into(new ArrayList<>(), (results, e) -> {
      Document document = new Document("chats", results);
      System.out.println(document.toJson());
    });

Scala code:

mongoCollection.aggregate(aggregatePipeline)
    .map[Document](doc => Document("chat" -> doc.get("_id"), "count" -> doc.get("count")))
    .subscribe((doc: Document) => println(doc.toJson))

As can be seen in the code in Scala, I'm not accumulating the aggregation results, because I do not know how to get the same behavior from the .into() method present in the Java code, using the MongoDB Scala Driver. I've done a lot of research on the internet, but without success. If anyone can help me, I appreciate it.

You should use the implicit Observable helpers specifically collect() . There is also a toFuture() method that effectively runs collect and returns the result as a Future .

mongoCollection.aggregate(aggregatePipeline)
    .map[Document](doc => Document("chat" -> doc.get("_id"), "count" -> doc.get("count")))
    .collect()
    .subscribe((docs: Seq[Document]) => println(docs))

You can setup a variable of Seq[Document] type and then append the resulting document sequence to the variable once the subscription event fires. Use Promise/Future to wait for the result. For example:

def find_all (collection_name: String): Seq[Document] = {

    /* The application will need to wait for the find operation thread to complete in order to process the returned value. */

    log.debug(s"Starting database find_all operation thread")

    /* Set up new client connection, database, and collection */
    val _client: MongoClient = MongoClient(config_client)
    val _database: MongoDatabase = _client.getDatabase(config_database)
    val collection: MongoCollection[Document] = _database.getCollection(collection_name)

    /* Set up result sequence */
    var result_seq : Seq[Document] = Seq.empty

    /* Set up Promise container to wait for the database operation to complete */
    val promise = Promise[Boolean]

    /* Start insert operation thread; once the thread has finished, read resulting documents. */
    collection.find().collect().subscribe((results: Seq[Document]) => {
      log.trace(s"Found operation thread completed")
      /* Append found documents to the results */
      result_seq = result_seq ++ results
      log.trace(s" Result sequence: $result_seq")

     /* set Promise container */      
     promise.success(true)

     /* close client connection to avoid memory leaks */
      _client.close
     })

    /* Promise completion result */
    val future = promise.future

    /* wait for the promise completion result */
    Await.result(future, Duration.Inf)
    /* Return document sequence */
    result_seq

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM