by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Ken Fehling
    @kenfehling
    I'm getting the "generation for id property type not supported" error, and from what I read in the source code, it seems that the case of Id is excluded (only String, ObjectId, etc. are covered)
    Ken Fehling
    @kenfehling
    Could it check if the type is StringId and then do something like StringId(ObjectId.get().toString())?
    Luis Miguel Mejía Suárez
    @BalmungSan
    @zigzago Apologies if I am not understanding correctly.
    But you are telling me I should just pass the whole query as a String?
    zigzago
    @zigzago

    @BalmungSan In KMongo, you have two modes
    The shell mode where you pass the query as string:

            """[ (...)
                { "${'$'}group" :
                  {
                    "_id" : {
                      "a" : "${'$'}stringField",
                      "date" : { "$dateToString" : { "format" : "%Y-%m-%d", "date" : "${'$'}dateField" } }
                    }
                  }
                }
                ]
            """)

    Look at http://litote.org/kmongo/extensions-overview/#aggregate & http://litote.org/kmongo/mongo-shell-support/ for more details

    @BalmungSan And the "typed query" mode.
    For example:

    class Projection(val stringField: String, val dateField: Date)
    class ResultKey(val a: String, val date: String)
    class Result(val _id: ResultKey)
    group(
                    fields(
                        ResultKey::a from Projection::stringField,
                        ResultKey::date from Projections.computed(
                            "$dateToString",
                            fields(
                                Projections.computed("format", "%Y-%m-%d"),
                                Projections.computed("date", Projection::dateField)
                            )
                        )
                    )
                )

    See https://litote.org/kmongo/typed-queries/ . The "typed query" mode is not yet really documented - this is planned for the next version

    Luis Miguel Mejía Suárez
    @BalmungSan
    OK, because the idea was to keep using the typed mode whenever possible.
    Also, could you give it a look to my second question? The one about the push
    zigzago
    @zigzago
    Of course the typed mode is better. But is is currently in "beta mode" as mentioned in the documentation. So you have to look at the unit tests "TypedTest" in the code for now: https://github.com/Litote/kmongo/tree/master/kmongo-core-tests/src/main/kotlin/org/litote/kmongo
    Luis Miguel Mejía Suárez
    @BalmungSan
    Yeah those tests have been my salvation those days :D
    zigzago
    @zigzago
    For the push you can use
    DataClass::records.push(combine(Record::field from Source::path, Record::field2 from Source::path2)))
    Luis Miguel Mejía Suárez
    @BalmungSan
    Cool!
    Thank you very much @zigzago!
    The library has been amazing to use. A little bi more of documentation and examples and everything would have been perfect.
    But as a mainteiner myself I know it is hard to keep the docs update.
    zigzago
    @zigzago
    You're welcome. Don't hesitate to add feature requests in github. In my sample, "Projections.computed" is ugly so I'm going to add a a fun String.from extension to replace it. And the documentation for typed queries is planned for the next release ;)
    Luis Miguel Mejía Suárez
    @BalmungSan
    :+1:
    zigzago
    @zigzago

    @kenfehling Please fill an issue and I will fix it ASAP. On my own projects I prefer to use the pattern

     class Data(val _id:Id<Data> = newId())

    because generateIdIfAbsentFromDocument Id generation requires that _id is var, or reflection hack. But as KMongo supports it for String and ObjectId, it has to be fixed ;)

    Ken Fehling
    @kenfehling
    @zigzago Ok thanks! I’ve submitted an issue: Litote/kmongo#162
    feherrer21
    @feherrer21
    Good afternoon, does anyone have documentation or an example for Lookup operator using let?
    zigzago
    @zigzago
    @feherrer21 I don't understand the question. What is the query you try to write?
    feherrer21
    @feherrer21
    MongoDB has two possibilities to use the Lookup Operator, one of them is using let and extra pipe line, i realized that Kmongo doesn't support the Lookup (let), only is possible using java api (aggregate) after 3.6 version, it is the query that i did
                 Aggregates.lookup(
                                "evaluationsAnswers",
                        listOf(Variable("questions", EvaluationsForms::questions)
                                ),
                        listOf(
                                match(expr(Projections.computed(
                                                "\$and", listOf(
                                                Projections.computed("\$in", listOf(EvaluationsAnswers::questionId, "\$\$questions")),
                                                Projections.computed("\$eq", listOf(EvaluationsAnswers::evaluated, ldapId))
                                                )
                                            )
                                        )
                                    ),
                                unwind("\$answers"),
                                match(expr(Projections.computed(
                                            "\$and", listOf(
                                                Projections.computed("\$eq", listOf(EvaluationsAnswers::answers.div(Answer::alreadyUsed), false)),
                                                Projections.computed("\$gte", listOf(EvaluationsAnswers::answers.div(Answer::answerDate), fromDate))
                                                )
                                            )
                                        )
                                    ),
                                group(
                                        fields( EvaluationRequest::userId from EvaluationsAnswers::answers.div(Answer::evaluator),
                                                EvaluationRequest::evaluationDate from Projections.computed(
                                                        "\$dateToString", fields(
                                                        Projections.computed("format", "%Y-%m-%d"),
                                                        Projections.computed("date", EvaluationsAnswers::answers.div(Answer::answerDate))
                                                    )
                                                )
                                            )
                                    )
                        ),
                            "results"
                    )
    zigzago
    @zigzago
    @feherrer21 KMongo now supports lookup operator - look at this unit test: https://github.com/Litote/kmongo/blob/master/kmongo-core-tests/src/main/kotlin/org/litote/kmongo/AggregateTypedTest.kt#L300 - next release with the lookup enhancement is planned in two weeks
    Piyush Ranjan
    @piyushranjan
    hello. I am new to kmongo so please excuse if this is a stupid question. I am trying to filter a collection on two keys. Something like this:
                val col = MongoWrapper.MONGO_DATABASE.getCollection<Ledgers>()
                val ledgers : FindIterable<Ledgers> = col.find(Ledgers::companyGuid eq cguid, Ledgers::status eq "not_synced"))
    however it seems find doesn't take more than one parameters. How do I achieve this ? Thanks in advance
    zigzago
    @zigzago
    @piyushranjan col.find( and (Ledgers::companyGuid eq cguid , Ledgers::status eq "not_synced")) should work
    tomotomo
    @tomocrafter
    I'm sorry if it is already referenced, but How can I update a field?
    Piyush Ranjan
    @piyushranjan
    @zigzago Thanks! It works. I wish more examples were there on the website
    zigzago
    @zigzago
    @tomocrafter col.updateOne(friend::name eq "Paul", set(friend::name, "John"))
    tomotomo
    @tomocrafter
    @zigzago Thank you! but what different between KMongo-property's KProperty<T>.setTo(T) and set(KProperty<T>, T)?
    zigzago
    @zigzago

    @tomocrafter Well set() is deprecated so my answer is not really ok... Both are equivalent:

    • setValue(Friend::name, "John")
    • Friend::name setTo "John"

    If you need to update more than one field, you can also write :

    • set( Friend::name setTo "John", Friend::age setTo 25)
    tomotomo
    @tomocrafter
    @zigzago Thank you so much!
    dcracauer
    @dcracauer
    Hi - new to KMongo, so far loving it. Trying to figure out how to register a Jackson Module and getting stuck. I'm sure its easy, can anyone help?
    Denis Kilchichakov
    @augur
    Hi Guys! Any ideas how to execute scripts on MongoDB 4.2? As eval() was removed...
    zigzago
    @zigzago
    @augur you can use the shell ( http://pauldone.blogspot.com/2019/05/mongo-shell-script-inside-bash.html ). But with the driver, there is no way to execute scripts anymore I think (see https://jira.mongodb.org/browse/SERVER-20510 )
    dcracauer
    @dcracauer
    @zigzago Thanks! worked great.
    feherrer21
    @feherrer21

    Hi everybody, i'm trying to convert this mongo update many to Kmongo.

    db.evaluationsAnswers.updateMany( { "answers._id": { $in: answerIds } }, { $set:{ "answers.$[].alreadyUsed": true}})

    (answerIds is an input String array)

    I tried this:

    collection.updateMany(
      Projections.computed(EvaluationsAnswers::answers.div(Answer::_id).path(), Projections.computed("\$in", answerIds)),
      set(SetTo(EvaluationsAnswers::answers.div(Answer::alreadyUsed), true)
    )

    But it doesn't work, I believe it is because of the positional update $[].
    Any idea about what i'm doing wrong and how to fix it?

    Thanks in advance.

    feherrer21
    @feherrer21
    Hi after a looong day I found this solution:
    collection.updateMany(
      Projections.computed(EvaluationsAnswers::answers.div(Answer::_id).path(), Projections.computed("\$in", answerIds)),
      set(SetTo(EvaluationsAnswers::answers.colProperty.posOp.div(Answer::alreadyUsed), true)),
      mongoUpdateCallback(expectedCount = answerIds.size.toLong(), handler = handler, logger = logger)
    )
    zigzago
    @zigzago

    @feherrer21 Here is a better solution:

    data class EvaluationAnswer(val answers:List<MyAnswer>)
    data class MyAnswer(val _id:String, val alreadyUsed: Boolean)
    
        fun test() {
            val answerIds:List<String> = listOf("a")
            col.updateMany(
                (EvaluationAnswer::answers / MyAnswer::_id) `in` answerIds,
                setValue(EvaluationAnswer::answers.colProperty.allPosOp / MyAnswer::alreadyUsed, true)
            )
        }

    I'm going to add it in the doc ;)

    zigzago
    @zigzago
    I'm also going to add a shortcut to remove the need of colProperty and mapProperty (in the next version - released soon)
    Luis Miguel Mejía Suárez
    @BalmungSan
    Cool :+1:
    feherrer21
    @feherrer21
    @zigzago nice thank you
    John Grange
    @grangej
    Are there any good examples on how to setup kotlinx serializable ? I am getting the following error : java.lang.NoClassDefFoundError: kotlinx/serialization/builtins/AbstractDecoder
    zigzago
    @zigzago
    @grangej you just have to add kmongo-serialization dependency artifact and annotate the class with @Serializable (in this sample http://litote.org/kmongo/quick-start/#lets-start-coding - the class Jedi). 3 days ago, there was a new kotlinx serialization release that is not compatible with the previous one : if you use kotlin 1.3.61 select kmongo 3.12.1, if you use kotlin 1.3.70 select kmongo 3.12.2. HTH
    John Grange
    @grangej
    @zigzago yes finally after much trial and error I was able to get the versions updated (had to update both serialization and maven-serialization). The frustration came from not knowing if my code was correct in the first place since I couldn't find any example of what it should look like :-)
    zigzago
    @zigzago
    artem-bakuta
    @artem-bakuta

    Hello, maybe somebody can help me, I'm using Mongo Compass request:

    {"orderExpDate":{$gte: new Date(new Date().setHours(new ISODate().getHours(),0,0)), $lte: new Date(new Date().setHours(new ISODate().getHours(),59,59))}}

    and it works correct, but using in Kmongo as col.findOne(//request here//) causes an Exception. What could be wrong ??? Thx