I'm trying to include the $setOnInsert field for Update and FindAndModify in ReactiveMongo. However, I can't seem to get it to work with the built in Commands.
Does anyone know how to include this field for updating a document?
This simple example works for me:
val collection = ... // your collection goes here
collection.update(
obj("_id" -> "1"),
obj(
"$setOnInsert" -> obj("field 1" -> "value 1"),
"$set" -> obj("field 2" -> "value 2")),
upsert = true
).map {
case n => println(n.ok.toString) // do smth
}
Related
I'm new to Elm and I ran into this problem...
We get translations for our page using something like:
case (translate translation.id) of
Success: -> translation
Failure: -> translation.id
Where translate just finds translation.id in a dictionary and it may or may not be there.
There are no runtime errors because you get a string either way, but we would like to log the missing translation to a rest service logger. But Elm hates side effects in the view that doesn't stem from html events so I'm not sure how to handle this.
Obviously in regular JS you could just crowbar in a fetch inside the failure case block and then return a string afterwards but that doesn't seem to be possible in Elm?
You need to move the effects part of your code into the update function. In this case I suggest doing it when the translation arrives. There you will want something like
OnTranslation translation ->
( { model | translation = translation } -- attach to model
, case translate translation.id of
Ok _ ->
Cmd.none
Err err ->
-- register with the error logger
logMissingTranslation translation.id
)
#Odin Thorsen, use Html.node and Html.Attributes.attribute to reference a custom element in Elm:
-- VIEW
view : Model -> Html.Html ()
view model =
Html.div []
[ translation model "key"
, translation model "junk key"
]
translation : Model -> String -> Html.Html ()
translation { translations } id =
Html.node "translated-text"
[ Html.Attributes.attribute "translation-id" id
, Html.Attributes.attribute "translation-text" <| translate translations id
]
[]
translate : Dict.Dict String String -> String -> String
translate translations id =
case Dict.get id translations of
Just value ->
value
Nothing ->
""
And in Javascript, use customElements.define to create the custom element:
customElements.define("translated-text", class extends HTMLElement {
constructor() { super(); }
connectedCallback() { }
attributeChangedCallback() { this.setTextAndLogFailure(); }
static get observedAttributes() { return ["translation-id", "translation-text"]; }
setTextAndLogFailure() {
var id = this.getAttribute("translation-id");
var text = this.getAttribute("translation-text");
if (text === null) return;
this.textContent = text;
if (!text.length) alert("Unkown translation id: " + id);
}
});
You'd replace alert("Unkown translation id: " + id); with the logging fetch.
Here is an Ellie with a full solution.
My DataWeave code looks like below:-
Result: Data reduce (item,ls={}) -> ls ++ From: {dev: item.warehouse}
Is there way to check the index of item object. I need to do conditional based on the index of the item object.
Example:
Item = Data[0] do this ;
Result: Data reduce (item,ls={}) -> ls ++ From: {dev: item.warehouse}
Item = Data[1] do this ;
Result: Data reduce (item,ls={}) -> ls ++ To: {dev: item.warehouse}
Original Code looks like below:
Result : ( Data reduce (
item,ls={}) -> ls ++
From:{id: "111",(if (item.sign == "333") {status: "OPEN"} else if (item.sign == "444") {status: "HOLD"} else {status: item.sign})}
I need to add "From" whenever the index of Item is odd number and add "To" whenever the index of item is even.
Since I don't have the conditional, I am always getting "From"
No you can't access any indexes, here's the documentation of reduce https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-reduce
What you can do is count the items yourself by modifying the structure of your accummulator: ls={counter=0,data={}}
Now you can use the counter to add one per iteration and keep track of things: {counter: ls.counter + 1, data: ls.data ++ To: {dev: item.warehouse}}
As you can understand you would need to add a conditional to differentiate between the From and To.
If I have time later on I 'll do it for you, or somebody else can beat me to it.
EDIT: here's the best I can do based upon your question, but you should get the idea:
%dw 2.0
output application/dw
var inputdata = [{warehouse: 100},{warehouse: 56}, {warehouse:1000}]
---
inputdata reduce (
(e, acc={c: 0, data: {From: {}, To: {}}}) ->
{
c: acc.c+1,
data: {
From: if (isEven(acc.c)) (acc.data.From ++ {warehouse: e.warehouse}) else acc.data.From,
To: if (isEven(acc.c)) acc.data.To else (acc.data.To ++ {warehouse: e.warehouse})
}
}
)
Always provide appropriate sample inputs and outputs of your transformation if you want to get the most out of the DW SO community ;)
I am playing a little bit with Elm these days, but I stuck with a simple case where I want to update a record field. My code is like this:
-- MODEL
initialModel : Model
initialModel =
{ selectedLanguage = "german"
, allCards = Card.cards
}
type alias Msg =
{ description : String
, data : String
, id : String
}
The update function
update : Msg -> Model -> Model
update msg model =
case List.head (model.allCards) of
Just card ->
{ card | fliped = True }
Nothing -> model
but I see this:
Something is off with the 1st branch of this `case` expression:
50| { card | fliped = True }
^^^^^^^^^^^^^^^^^^^^^^^^
The 1st branch is a record of type:
{ back : String, fliped : Bool, front : String, id : String }
But the type annotation on `update` says it should be:
Model
Hint: Seems like a record field typo. Maybe back should be allCards?
Hint: Can more type annotations be added? Type annotations always help me give
more specific messages, and I think they could help a lot in this case!
Detected errors in 1 module.
I think I should always return a model from update function like my type says, but cannot figure out how. Any advice here?
You'll have update the allCards field of model too. You can nest the card update inside the model update if the former returns a list instead of just a single card:
update : Msg -> Model -> Model
update msg model =
{ model
| allCards =
case model.allCards of
card :: rest ->
{ card | fliped = True } :: rest
[] ->
[]
}
Or you can bind the new allCards to a name if you prefer:
update : Msg -> Model -> Model
update msg model =
let
newAllCards =
case model.allCards of
card :: rest ->
{ card | fliped = True } :: rest
[] ->
[]
in
{ model | allCards = newAllCards }
I pattern match directly on the list here instead of using List.head, as that also gives me the remainder of the list and I don't have to deal with an intermediary Maybe value (or two actually, since List.tail returns a Maybe as well). The card::rest branch hits if allCards contains at least one card, so the only remaining case is therefore [], which is easy enough to handle.
Also, flipped is spelled with two ps ;)
I would like the following kotlin code to work:
val result: Try<Option<String>> = Success(Some("test"))
val test = when {
result is Success && result.value is Some -> result.value.t // not working
result is Success && result.value is None -> "Empty result"
result is Failure -> "Call failed!"
else -> "no match!"
}
I use the arrow library for the Try and Option monad.
Unfortunately, I can only access the value of the first condition "is Success" and not the second condition "is Some". So, I can only do "result.value", I then get an Option of String.
Am I missing something? This will save me alot of inner ".map" and ".fold" calls.
Update:
I need to cast it first, which is ugly:
result is Success && result.value is Some -> (result.value as Some<String>).t
I tried your example in IntelliJ with Kotlin 1.3.21.
It shows the reason of the problem:
You need to extract the result.value as a variable to make it work. I found the following snippet to solve it
val result: Try<Option<String>> = Success(Some("test"))
val test = when (result) {
is Success -> when(val value = result.value) {
is Some -> value.t
is None -> "None"
}
is Failure -> "Call failed!"
else -> "no match!"
}
I use Kotlin 1.3.x when with declaration syntax.
You may also use Arrow API to get similar result:
val test = result.fold(
ifSuccess = { it.getOrElse { "None" }},
ifFailure = { "Call failed!" }
)
Here you do not need to have the else clause in when.
You can simplify the pattern matching like this:
val test = result
.map { it.getOrElse { "Empty result"} }
.getOrElse { "Call failed!" }
Which is a bit more exhaustive and doesn't require an else alternative
Alternatively, if you don't care about the exception that is thrown you can use toOption on the Try:
val test = result
.toOption()
.getOrElse { "No value!!" }
However, that has some obvious loss of information.
I personally would bubble up the Try instance to the consumer of the result collapsing the inner Option with a .map so that the final result is of type Try<String> and let the consumer handle the error.
However, it depends a lot of the actual context of the problem.
Background:
I have attempted to accomplish the question defined here, and I have not been able to succeed. Acolyte requires you to define the queries and parameters you want to handle within a match expression, and the values used in match expressions must be known at compile time. (Note, however, that this StackOverflow answer appears to provide a way around this limitation).
If this is indeed not possible, the inability to dynamically define the parameters and queries for Acolyte would be, for my use case, a severe limitation of the framework. I suspect this would be a limitation for others as well.
One SO user who has advocated for the use of Acolyte across a handful of questions stated in this comment that it is possible to dynamically define queries and their responses. So, I have opened this question as an invitation for someone to show that to be the case.
Question:
Using Acolyte, I want to be able to encapsulate the logic for matching queries and generating their responses. This is a desired feature because I want to keep my code DRY. In other words, I am looking for something like the following pseudo-code:
def generateHandler(query: String, accountId: Int, parameters: Seq[String]): ScalaCompositeHandler = AcolyteDSL.handleQuery {
parameters.foreach(p =>
// Tell the handler to handle this specific parameter
case acolyte.jdbc.QueryExecution(query, ExecutedParameter(accountId) :: ExecutedParameter(p) :: Nil) =>
someResultFunction(p)
)
}
Is this possible in Acolyte? If so, please provide an example.
It is indeed possible to parameterize queries and/or parameters by utilizing pattern matching.
See the code below for an example:
import java.sql.DriverManager
import acolyte.jdbc._
import acolyte.jdbc.Implicits._
import org.scalatest.FunSpec
class AcolyteTest extends FunSpec {
describe("Using pattern matching to extract a query parameter") {
it("should extract the parameter and make it usable for dynamic result returning") {
val query = "SELECT someresult FROM someDB WHERE id = ?"
val rows = RowLists.rowList1(classOf[String] -> "someresult")
val handlerName = "testOneHandler"
val handler = AcolyteDSL.handleQuery {
case acolyte.jdbc.QueryExecution(`query`, ExecutedParameter(id) :: _) =>
rows.append(id.toString)
}
Driver.register(handlerName, handler)
val connection = DriverManager.getConnection(s"jdbc:acolyte:anything-you-want?handler=$handlerName")
val preparedStatement = connection.prepareStatement(query)
preparedStatement.setString(1, "hello world")
val resultSet = preparedStatement.executeQuery()
resultSet.next()
assertResult(resultSet.getString(1))("hello world")
}
it("should support a slightly more complex example") {
val firstResult = "The first result"
val secondResult = "The second result"
val query = "SELECT someresult FROM someDB WHERE id = ?"
val rows = RowLists.rowList1(classOf[String] -> "someresult")
val results: Map[String, RowList1.Impl[String]] = Map(
"one" -> rows.append(firstResult),
"two" -> rows.append(secondResult)
)
def getResult(parameter: String): QueryResult = {
results.get(parameter) match {
case Some(row) => row.asResult()
case _ => acolyte.jdbc.QueryResult.Nil
}
}
val handlerName = "testTwoHandler"
val handler = AcolyteDSL.handleQuery {
case acolyte.jdbc.QueryExecution(`query`, ExecutedParameter(id) :: _) =>
getResult(id.toString)
}
Driver.register(handlerName, handler)
val connection = DriverManager.getConnection(s"jdbc:acolyte:anything-you-want?handler=$handlerName")
val preparedStatement = connection.prepareStatement(query)
preparedStatement.setString(1, "one")
val resultSetOne = preparedStatement.executeQuery()
resultSetOne.next()
assertResult(resultSetOne.getString(1))(firstResult)
preparedStatement.setString(1, "two")
val resultSetTwo = preparedStatement.executeQuery()
resultSetTwo.next()
assertResult(resultSetTwo.getString(1))(secondResult)
}
}
}