Monday, May 7, 2018

Http java links

https://www.programcreek.com/java-api-examples/?api=io.reactivex.netty.protocol.http.client.HttpClient

https://blogs.oracle.com/java/jdk-http-client#synchronousGet

http://www.baeldung.com/java-http-request

https://stackoverflow.com/questions/46387896/how-can-i-use-http-client-apisince-java-9-in-java8-project

https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

http://openjdk.java.net/groups/net/httpclient/intro.html

https://alvinalexander.com/java/java-apache-httpclient-restful-client-examples

https://github.com/timboudreau/netty-http-client

https://github.com/cloudbow/netty-http-client

TODO mongodb query toJson



Operator $all produces error on toJson() method.

http://api.mongodb.com/java/current/org/bson/codecs/package-summary.html

https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver

http://api.mongodb.com/java/3.0/?com/mongodb/client/model/Filters.html

http://mongodb.github.io/mongo-java-driver/3.4/bson/codecs/


Criteria criteria = Criteria.where("props").all(
new Criteria().elemMatch(Criteria.where("key").is("lang").and("value").is("EN")),
new Criteria().elemMatch(Criteria.where("key").is("photo").and("value").exists(true)));
String json = criteria.getCriteriaObject().toJson();



org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.springframework.data.mongodb.core.query.Criteria.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:184)


Documentation: $all with $elemMatch

If the field contains an array of documents, you can use the $all with the $elemMatch operator.
The following operation queries the inventory collection for documents where the value of the qty field is an array whose elements match the $elemMatch criteria:
db.inventory.find( {
                     qty: { $all: [
                                    { "$elemMatch" : { size: "M", num: { $gt: 50} } },
                                    { "$elemMatch" : { num : 100, color: "green" } }
                                  ] }
                   } )

Thursday, May 3, 2018

Count how much unique field values in mongo

db.getCollection('myCollection').aggregate( 
    {$group: {"_id":"$username"}},
    {$group: {"_id":1, count:{$sum:1}}
})

db.getCollection('myCollection').aggregate( [
    {$match: { "_id.appId":"23893259736", "status" : 1 } },
    {$group: {"_id":"$_id.deviceId"}},
    {$group: {"_id":1, count:{$sum:1}}}

])


Criteria criteria = SearchQueryBuilder.parse(query);
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(criteria),
Aggregation.group("username"),
Aggregation.group("username").count().as("count")
);
try {
AggregationResults aggregationResults = mongoTemplate.aggregate(aggregation, Devices.class, GroupAndCount.class);
GroupAndCount cbg = aggregationResults.getMappedResults().iterator().next();
return cbg.count;
} catch (Exception e) {
return 0;
}

private static final class GroupAndCount {
@Id
private Integer _id;
private Integer count;
}