MongoDB ODM framework mongomongo-simplifies your data storage

Source: Internet
Author: User

Mongmongo is an ODM framework written in Java, making MongoDB operations more convenient.

 

Mongomongo strives to provide Java developers with functions similar to activeorm or
The Hibernate operation API retains MongoDB's schemaless, document-based design, dynamic query, atomic modification operations, and other features. Of course, you can easily bypass mongomongo and use the functions provided by Java driver native.

 

The following is an example code:

 

 

public class Blog extends Document {    static {        storeIn("blogs");                        hasManyEmbedded("articles", new Options(map(                Options.n_kclass, Article.class        )));        //create index        index(map("blogTitle", -1), map(unique,true));        //validate uerName field        validate("userName",map(length,map(                minimum,5        )));    }    //association related    public AssociationEmbedded articles() {throw new AutoGeneration();}    private String userName;    private String blogTitle;}public class Article extends Document {    static {                belongsToEmbedded("blog", new Options(map(                Options.n_kclass, Blog.class        )));    }    public AssociationEmbedded blog() {throw new AutoGeneration();}    private String title;    private String body;}public class Usage{  public static void main(String[] args){     Blog blog = Blog.where(map("userName","sexy java")).in(map("id",list(1,2,3))).singleFetch();     blog.articles().build(map("title","i am title","body","i am body"));     blog.save();  }}

 

We can see that this is a typical congestion model. Operations such as association, storage, index creation, and alias setting can be implemented simply by calling a function in the static block. If you use some dynamic languages, you will find that this method-level declarative syntax is very popular and comfortable to write.

 

In fact, there are already many MongoDB-related frameworks. What are the advantages of mongomongo? We can easily compare the code.

 

Taking springdata for MongoDB as an example, the typical operations are as follows:

 

 public static void main( String[] args )    {        MongoOperations mongoOps = new MongoTemplate(new Mongo(), "mydb");        Person person = new Person();        person.setName("Joe");        person.setAge(10);        mongoOps.insert(person);        log.info(mongoOps.findOne(new Query(Criteria.where("name").is("Joe")), Person.class));    }

 

 

 

In fact, most Java ODM operate models in this way. In order to construct a query string to introduce the criteria object, in order to carry out the query and introduce the query object, the person object must be affirmed during the query.

In addition, the index, alias, and verification settings are cumbersome and unclear. Generally, the information is placed on the annotation of the field, or some configuration objects are set.

Mongomongo puts most collection configurations in the code block of the model, which is clear and easy to manage.

 

Corresponding mongomongo code

 

public static void main( String[] args ) {     Person person =  Person.create(map("name","Joe","age",34));     person.save();     log.info(Person.where(map("name","Joe")).singleFetch());  }

 

 

 

Mongomongo query is also a typical ORM operation method.

 

Blog blog = Blog.where(map("active",true)).in(map("id",list(1,2,3))).singleFetch();

 

You can also write it as follows:

public class Blog extends Document {   public Criteria active(){    return where(map("active",true));   } }

 

Then you can call:

List<Blog> blogs = Blog.active().where(map("userName","jack")).fetch();

 

High reusability.

If you have used activeorm, perform this operation on MongoDB.
You will feel quite comfortable.

Mongomongo is currently used in some internal small projects. It is customized according to requirements. Therefore, it is not perfect for activeorm, and many details and functions are not taken into account. However, you can use the MongoDB exposed by it.
Driver Solution.

 

For example:

 

TTUser.collection().find(new BasicDBObject("tagName","cool"));

 

In this case, you use the collection () method to obtain the dbcollection object.

 

Of course, I also hope to meet a complicated project in the future, so that I can improve mongomongo in this project.

If you use it in your own project, I think it will save a lot of code.

 

Follow the 5 steps to run a application on your Mongo experience.

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.