Monday, May 7, 2018

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" } }
                                  ] }
                   } )

No comments: