bguerout / jongo Goto Github PK
View Code? Open in Web Editor NEWQuery in Java as in Mongo shell
Home Page: http://jongo.org
License: Apache License 2.0
Query in Java as in Mongo shell
Home Page: http://jongo.org
License: Apache License 2.0
Hello,
Is there a connection pool in Jongo? Or, if not, how can I find one?
Unknown properties can be handled with @JsonAnySetter (Jackson fallback annotation)
private static class BackwardFriend extends Friend {
Date oldDate;
@JsonAnySetter
public void fallbackForBackwardCompatibility(String name, Object value) {
if ("oldAddress".equals(name)) {
setAddress((String) value);
}
}
}
Nevertheless we should natively provide a way to adpat JSON before unmarshalling
Opération en écriture
$near
{name : # }
{names :{ $in :#}} => [1,2,3]
Given a POJO with a 'java.util.Date' attribute :
Jackson marshalls this attribute as a timestamp,
Field is persisted as a NumberLong into Mongo instead of ISODate
When dealing with pre-existing documents (ie. non persisted with Jongo), date are ISODate (eg. {$date:"2011-10-12T14:54:02.069Z"}) and Jackson is not able to unmarshall them.
This bug can occur on all BSON Primitives because (Un)Marshalling are fully delegated to Jackson.
L'utilisation d'une interface ou d'un sous-type lors de la persistance, pose problème lors de l'accès à la donnée. Morphia ajoute un attribut « className » dans les entités de ce type.
jackson : http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
morphia : http://www.carfey.com/blog/tag/nosql
une autre implem : https://github.com/dadastream/lib-mongomapper
I don't think many people will use it, but I needed it at one point and was quite surprised that there is no such thing.
After a save operation, ObjectId generated by MongoDB is setted into POJO Object using reflection.
All POJO attributes are scanned and when an ObjectId type is found, we set it even if it doesn't represent document id.
We should finf way to only set document id....
Here is a failling test
@Test
public void shouldNotChangeOtherObjectIdField() throws IOException {
ObjectId relationId = new ObjectId();
LinkedFriend friend = new LinkedFriend(relationId);
collection.save(friend);
assertThat(friend.friendRelationId).isNotEqualTo(friend.getId()); //fail
assertThat(friend.friendRelationId).isEqualTo(relationId);
}
private static class LinkedFriend extends Friend {
private ObjectId friendRelationId;
private LinkedFriend(ObjectId friendRelationId) {
this.friendRelationId = friendRelationId;
}
}
'#' is used to replaced token in a query.
API User should be able to create a query with a custom token (eg. @ or ?)
We should provide a marshaller and an umarshaller to handle document as bytes[]
I know this is not the most efficient way todo it but i want to update a complete document using:
collection.update("{ _id: # }", id).upsert().with("#", myObject);
This doesn't work but (" # ", myObject)
does.
Possible feature: it would be nice to have a .with() which accepts a (T object) just as with .save()
Supports named parameters , jpql example:
SELECT pub FROM Publisher pub WHERE pub.revenue>: rev
jongo :
friends.find("{name: #name, age: #age}")
.bind("name","John")
.bind("age",18)
;
On find().as(MyObject.class) , Jongo should add partial fields based on MyObject fields.
On find().fields({xx}) .as(MyObject.class), Jongo should use json 'fields' value
Save operation returns generated ID so WriteResult is not available.
We have to find a way to allow user to acces to WriteResult + generated Id
Few options:
(un)marshalling is based on two interfaces org.jongo.marshall.Unmarshaller and org.jongo.marshall.Marshaller
However, theses 2 interfaces don't explain clearly what a client have to implement to provide full compliant (un)marshallers
Example: Behind the scene, a Marshaller have to be able to serialize ObjectId...
Even if we do not change interfaces, we should provide a (un)marshalling compatibility test
@beatKyo is prototyping Gson (un)marshallers
Problem in two words: org.jongo.marshall.jackson.JacksonProcessor serializes any byte[] field as (without quotes) so it breaks data integrity and moreover produces invalid JSON.
Roots of the problem are located in org.jongo.marshall.jackson.NativeSerializer, and, deeply, in mongodb driver.
com.mongodb.util.JSONSerializers.LegacyBinarySerializer looks like:
private static class LegacyBinarySerializer extends AbstractObjectSerializer {
@Override
public void serialize(Object obj, StringBuilder buf) {
buf.append("<Binary Data>");
}
}
For more information, see https://groups.google.com/forum/?hl=fr&fromgroups#!topic/jongo-user/SjK1KwSErEs
As described in th documentation, a POJO with an ObjectId named '_id' doesn't need to be annotated with JSONProperty
Given a POJO :
public static class User {
private ObjectId _id;
private String name;
public ObjectId getId() {
return id;
}
public void setId(ObjectId _id) {
this.id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Jackson handle '_id' and getId() as two different properties.
User is serialised as { "_id" : "4f92d1ae44ae2dac4527d49b" , "name" : "johnny" , "id" : { "$oid" : "4f92d1ae44ae2dac4527d49b"}}
During deserialization, Jackson sets '_id' property with ''_id" field and tries to set 'id' property using a new ObjectId()
javax.persistence ?
MongoIterator
getDb()
getCollection()
sort, order, distinct
User should be able to reuse Jongo's jackson configuration.
An custom ObjectMapper can be injected into JacksonProcessor but it's a bit tricky to enhance configuration.
collection.findOne("{status:#}",Status.TEST).as(PojoWithEnum.class);
Hi guys,
I receive this when working with aggregate:
{$group : {key: {topic:1, recipient:1},reduce: function(obj,prev){if(!obj.hasOwnProperty("topic")){prev.topic=obj.topic;prev.recipient=obj.recipient}}, initial: { } } } cannot be parsed
The same thing works perfectly well in the monogo console. That's why I think it might be an issue with Jongo. Any idea what's going on wrong here?
collection.find().sort("{'address':1}").as(Friend.class);
should be expressed as follow :
collection.find().sort("{'address':#}", 1).as(Friend.class);
Instead of
collection.update("{_id:#}", myId ).with("{$set:#}", pojo);
user should update a document without a parameterized query
collection.update(myId).with("{$set:#}", pojo);
When parameters are binded into query, Jongo use native serialization feature ((ie; mongo java driver: Jongo.serialize()).
This serialization mechanism works well when parameters are BSON Primitives (List, String, int ...) but fail when parameter is a complex type :
collection.update('{}',{'$push:{value:#}},myBean)
User should be able to provide a custom behaviour to serialize his types.
public MongoCollection(String database, String collection) throws UnknownHostException, MongoException {
this.collection = new Mongo().getDB(database).getCollection(collection);
this.convertor = new DBObjectConvertor();
}
MongoCollection class is declared with a final keyword to prevent subclassing.
Nevertheless a final class can not be easily mocked (eg. with Mockito)
Here is few solutions :
save, find, update, count
Suggested by Pedro Sanders : https://groups.google.com/forum/#!msg/jongo-user/5PYFSSCklqE/unm2ZP2LIdoJ
$greaterThan
$..
"{'$query':{}, '$orderby':{'address':1}}"
Suggested by @MrAdrenalin on Twitter : Is there any equivalence to Morphia's "Referencing Objects" annotation ?
Jongo should use DBRef Object provided by java driver (http://www.mongodb.org/display/DOCS/Database+References)
Mongo does not require entities to have ObjectId, _id property can be a simple String
A 0-dev solution should be to create a query with ObjectId
new Query.Builder("{_id:ObjectId(#)}").parameters(id);
new Query.Builder("{_id:#}").parameters(simpleId);
It's possible to ignore undesired properties of a document in a POJO. But, for every property manipulated, one must create a class with attribute (primitive or sub-class). It can be useful to access some property and sub-property as a raw object: an attribute containing a JSON object of every node and sub-nodes of a given node.
This stackoverflow question offers some clues on the matter.
@JsonProperty("_id") -> @id ?
javax.persistence.id ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.