Giter VIP home page Giter VIP logo

Comments (5)

tvd12 avatar tvd12 commented on May 27, 2024

You can not do that, you need to create an class for a result, please take a look here: https://youngmonkeys.org/ezydata/guides/ezymongo-custom-query-result

from ezydata.

Assambra avatar Assambra commented on May 27, 2024

I mean i do all required:

Do i need this file if i use a StringValueResult?
StringValue:

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class StringValue {
    @EzyId
    Long id;

    String email;
    String username;
    String password;
}

Custom Query:

@EzyRepository("accountRepo")
public interface AccountRepo extends EzyMongoRepository<Long, Account>{
@EzyQuery("{$query: {?0:'?1'},{_id:0, ?2:1}")
    StringValueResult getStringFieldByValue(String field, String value, String retValue);
}

StringValueResult:

@Data
@EzyQueryResult
@NoArgsConstructor
@AllArgsConstructor
public class StringValueResult {
    String username;
}

Call:

StringValueResult value = accountService.getStringFieldByValue("email", request.getEmail(), "username");
        logger.info(value.getUsername());

Same Error as i descriped before:
readStartArray can only be called when CurrentBSONType is ARRAY, not when CurrentBSONType is DOCUMENT.

is my Custom query right or allowed or what ive done wrong?
@EzyQuery("{$query: {?0:'?1'},{_id:1, ?2:1}")

And i ask before i want to create a universal function that i use in different ways and i dont want create for each single value create a own custom query, result class to get one time the username next time the emailaddress and so on. Now it the field is a username is der some solution if you take a look on the call:

StringValueResult value = accountService.getStringFieldByValue("email", request.getEmail(), "username");
StringValueResult value = accountService.getStringFieldByValue("someotherfield", someothervalue, "someotherfield");

from ezydata.

tvd12 avatar tvd12 commented on May 27, 2024

ah, now I understand you mean, so you can do like this:

  1. Create findStringFieldByValue in your repo
@EzyQuery("{$query: {?0:?1}, $fields: [?2], $orderBy: {_id:1, ?2:1}}")
Account findStringFieldByValue(String field, String value, String retValue);
  1. Get the the field you want

Because this query will use projection so you don't need to worry about performance, you can check logs to see, example:

09:11:53.736 [main] DEBUG com.tvd12.ezydata.mongodb.testing.bean.PersonRepo$EzyDatabaseRepository$EzyAutoImpl$6 - find one with query: {$query: {'personName':'dzung'}, $fields: ['personName'], $orderBy: {_id:1, 'personName':1}}
09:11:53.737 [main] DEBUG org.mongodb.driver.protocol.command - Sending command '{"find": "test_mongo_bean_person", "filter": {"personName": "dzung"}, "projection": {"personName": 1}, "limit": 1, "singleBatch": true, "$db": "test", "lsid": {"id": {"$binary": {"base64": "jjD1P/zhR+KgCDkmXbdsjg==", "subType": "04"}}}}' with request id 12 to database test

from ezydata.

Assambra avatar Assambra commented on May 27, 2024

Thank you, it works but not realy happy with this behaviour. But if you say there is no performance issue i can live with that. I tested it all and other fields are empty only the id and the requested field has values;

Maybe it is only in my head but it would be nice or some feature request vor a new standard query i mean there are lot of usecases for it to get simple values (int or string) by given field and value pair:

@EzyQuery("{$query: {?0:'?1'},{_id:0, ?2:1}")
getFieldValueByFieldAndValue(String field, String value, String fieldValue);

and to get the datas out with:

var value = accountService.getFieldValueByFieldAndValue("email", '[email protected]', username);

logger.info(value.getMyRequestedField());

Thanks a lot again to giving me your time.

from ezydata.

tvd12 avatar tvd12 commented on May 27, 2024

Actually, mongodb always return BsonObject (Document or Array) so return only single value is hard, but let me think, thank you.

from ezydata.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.