Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    zigzago
    @zigzago
    @augur merged
    hajanis
    @hajanis
    Hi guys , any idea about grouping multiple field with aggregation framework?
                match(TestingType::address / AddressType::state eq "Berlin"),
                group(listOf(TestingType::validity, TestingType::location))
            )
    $group: {
            _id: {
                validity: "$validity",
                consequence: "$consequence"
            }
    }
    this is what i am trying to achieve
    zigzago
    @zigzago

    @hajanis Sorry for the delay of the answer.

    class T(val p1:String, val p2:String)

    @Test
    fun test() {
        val g = group(fields(T::p1.from(T::p1.projection), T::p2.from(T::p2.projection)))
        println(g.json)
    }

    print {"$group": {"_id": {"p1": "$p1", "p2": "$p2"}}}

    HTH

    XLR
    @Tmpod_gitlab
    Hey there! I was wondering how you can tell KMongo to only return certain fields, like this shell cli query would: db.coll.find({}, {_id: 1})
    XLR
    @Tmpod_gitlab
    it's only sync tho :c
    forgot to mention I'm using coros
    XLR
    @Tmpod_gitlab
    will do, thanks!
    Nikita Rudy
    @MelonCode
    Hello everyone!
    Seeking for help
    I saw that KMongo can use Kotlinx Serialization
    But how do I specify Context?
    I want to pass serializers, but cannot find an example
    Nikita Rudy
    @MelonCode
    Uh... This looks pretty dead
    zigzago
    @zigzago
    Luis Miguel Mejía Suárez
    @BalmungSan

    Hello, first time using both Kotlin and KMongo. I am translating an aggregation pipeline from plain Mongo to Kotlin.
    I am having trouble with this part:

    { "group" : 
      {
        "_id" : {
          "a" : "$stringField",
          "date" : { "$dateToString" : { "format" : "%Y-%m-%d", "date" : "$dateField" } }
        }
      }
    }

    It seems KMongo doesn't suppoer the dateToString function. So I tried to just pass the entire _id expression as a String literal, but that also didn't worked. How can I tell KMongo that I want to pass a raw query?

    group("{ \"date\" : \"{ \"\$dateToString\" : { \"format\" : \"%Y-%m-%d\", \"date\" : \"\$dateField\" } } }")
    Luis Miguel Mejía Suárez
    @BalmungSan

    I fixed it like this:

    group(BsonDocument(listOf(
          BsonElement("a", BsonString("\$stringField")),
          BsonElement("date", BsonDocument("\$dateToString", BsonDocument(listOf(
                  BsonElement("format", BsonString("%Y-%m-%d")),
                  BsonElement("date", BsonString("\$dateField"))
          ))))
    )))

    Not sure if this is the best way, and I am surprised about the lack of documentation about how to create a simple document by hand.

    Luis Miguel Mejía Suárez
    @BalmungSan
    New problem, how to push an object with multiple arguments.
    Luis Miguel Mejía Suárez
    @BalmungSan

    After a lot of searching (because there wasn't any documentation) I found that on $group stage I can use push like:

    group(
      "id",
      DataClass::records.push(Record::field from "\$path")
    )

    But what if Record has many fields, how can I tell KMongo from where to get all its properties?

    Ken Fehling
    @kenfehling
    Hi! Thanks so much for this project, I've been having fun using it.
    I was wondering though, is there a technical reason why Id can't be generated?
    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