question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Find select field

See original GitHub issue

I have nations with settlements with members, I’m trying to get all of the members of the nation through the settlements.

        val members: List<SLPlayerId> = Settlement.COL
            .find(Settlement::nation eq id).projection(Settlement::members)
            .map { it.members }
            .flatten()

This gives me this error:

[08:01:22 ERROR]: [SLCore] [ACF] Caused by: com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class net.starlegacy.slcore.mongo.Settlement] value failed for JSON property territory due to missing (therefore NULL) value for creator parameter territory which is a non-nullable type
[08:01:22 ERROR]: [SLCore] [ACF]  at [Source: de.undercouch.bson4jackson.io.LittleEndianInputStream@24d5132b; pos: 148] (through reference chain: net.starlegacy.slcore.mongo.Settlement["territory"])

Is there a way to get the value without attempting to construct the whole object?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zigzagocommented, Dec 11, 2018

You have two options:

  1. project into Document (ie Map):
val members: List<SLPlayerId> = Settlement.COL
            .withDocumentClass<Document>()
            .find(Settlement::nation eq id)
            .projection(Settlement::members)
            .map { it.getString(Settlement::members.name) }
            .flatten()

This will not work if members is a complex object.

  1. project into dedicated object
data class SettlementWithMembersOnly(val members: Array<Array<SLPlayerId>>)

val members: List<SLPlayerId> = Settlement.COL
            .withDocumentClass<SettlementWithMembersOnly>()
            .find(Settlement::nation eq id)
            .projection(Settlement::members)
            .map { it.members }
            .flatten()

Also I’m going to add a projection extension method, in order to project “out of the box” one, two or three fields.

HTH

0reactions
MicleBrickcommented, Dec 12, 2018

Okay, hopefully you’ll find what I came up with helpful 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose, Select a specific field with find - Stack Overflow
The _id field is always present unless you explicitly exclude it. Do so using the - syntax: exports.someValue = function(req, res, ...
Read more >
Project Fields to Return from Query — MongoDB Manual
Use the Select your language drop-down menu in the upper-right to set the ... the db.collection.find() method returns all fields in the matching...
Read more >
How to select fields in a document in MongoDB
MongoDB is really easy to work with. Today, we will see how to select only few fields out of a large document using...
Read more >
Select fields (Concepts) - Prisma
This page explains how to select only a subset of a model's fields and/or include relations ("eager loading") in a Prisma Client query....
Read more >
Retrieve selected fields from a search | Elasticsearch Guide [8.5]
Use the docvalue_fields parameter to get values for selected fields. This can be a good choice when returning a fairly small number of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found