scala - MongoDB update a document when exists already with ReactiveMongo -


I am writing a Scala web application that uses MongoDibby as a database and as a reactive Mongo as a driver .

There is a collection named I Recommendation Correlation in which I had saved the relationship between a product and a category.

A document contains the following form:

   

I am now writing a method as the following: < Calculation of df calculation: future [boolean] = {calculation of df (category: string, tag: string, likeness: list [likeness]): future [(double, int)] = {println ("+ + Calculate the relationship of + + + + + + + + + tag ") Val Value = Equality. Fold Lift (0.0, 0) ((R, C)) = & gt; (C.tag1Name.split (":") (0) == category and c.tag2Name == tag) (r._1 + c.eq, r._2 + 1) other R) // tag value addition = Value ._1 va l calculation = value ._2 value result = if (count> 0) (zodiac / counting, counting) and (0.0, 0) future {result}} play.logger.debug ("start interaction ") Similarity: all.tostlestatmat {tag match => Val tuples = i {lt; - tag match} yield (i.tag1Name.split (": ") (0), i.tag2Name) // create e list Category and product name for [[string, string]) val r = map of tuples {l = & gt; Calculation (el._1, el._2, tag match) flatMap {value = & gt; Val correlation = correlation (el._1, el._2, value._1, value._2) // correlation wall query = Json.obj ("category" -> standard ._1, "attribute" -> standard Create a .2) correlations.find (query). A flatmap (element => element match {case some (x) => correlations.update (query, correlation) flatmap {status => status match {case LastError (OK, _, _, _, _, _, _, _, _, _, _, _) = & future;} {true} case _ = & gt; future {false}}} no case => Correlation.Sheave (correlation) flatmap {condition => position match {case end (fine, _, _, _, _, _, _) = Future {true} case _ = & gt; future {false}}}}}} val result = if (res.exists (_ equals)) wrong and true future [result}}

The problem is that the method involves duplicate documents.

I have resolved using "code" db.recommendation.correlation.ensureIndex ({"category": 1, "attribute": 1}, {"exclusive": "DropDups" : True}) , but how can I fix the problem without using index ??

What's wrong ??

One place you want to do is update. To do this with Reactiv Mongo, you have to use it to tell which fields will be updated, And how. Instead, you The update method of the enlightenment has passed to correlation (which I think is some kind of BSOND document). It only requests the replacement of the document, which is unique if the unique index value is different A new document has to be added. Instead of passing the correlation , you should pass a BSODD which is set to $ set (set field) or $ incr (incrementing one numeric field by one) U For information about doing this, please see


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -