MongoDB ODM Framework :MongoMongo-Simplifies your Storage

MongoMongo是为Java开发者设计的MongoDB ODM,它提供了熟悉且强大的API,利用MongoDB的无模式文档、高性能设计、动态查询和原子修改操作。本文通过示例展示了如何使用MongoMongo进行关联、存储声明、索引创建和验证配置,与现有ODM如morphia、SpringData进行了对比,并介绍了其静态块特性,方便、清晰地管理代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 MongoMongo is an Object-Document-Mapper (ODM) for MongoDB written in Java.

 The philosophy of MongoMongo is to provide a familiar API to Java developers who have been using ActiveORM or Hibernate, while leveraging the power of MongoDB's schemaless and performant document-based design,dynamic queries, and atomic modifier operations.

 

 

Sample 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();
  }

}

 

You can put assotiation,storage declare,index creating ,alias filed in static block of model,all can done by a method calling. Properties and Getter/Setter methods are optional.In sample code,pojo propertes declared make you know tha how many fields should be put in mongo collection.

 

There are already some ODM like morphia,SpringData.We can see diffrences bettween MongoMongo 

 

SpringData for MongoDB:

 

 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));
    }

 

 

Actually,most of java ODM operate model like this. Introduce Criteria Object for Query constructing, and Query Object for query. You also should tell 

MongoOperations who do you want to operate(here is Person.class). It's bit tough for index,alias,validation configuration,normaly they will put this infomation

on annotation,or som Configuration Object. MongMongo put all these in static block,using static method to declare,convenient,clear and easy to manager.

 

 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's query is similary with ActiveRecord in rails.

 

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

 

Normally i will write like this:

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

 

then you can call it like this:

 

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

 

If you have used ActiveORM,then you will be familiar with MongMongo.

 

You also can MongoDB Java Driver directly  like this:

 

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

 

you can call static method collection() to get DBCollection Object.

 

Try MongoMongo according to this article: 5 steps to run a application on MongoMongo

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值