Search code examples
javaentitiesannotated-pojos

POJOs as documents in mongodb


I am trying to save entity as documents in Mongodb and tried to create Codec for it . And it does not seem to work . I tried Morphia but, i am not able to find documentation on adding extra fields into document at runtime using morphia. this would defy the basic purpose of using Mongodb at first place . I initially want my documents to have fixed number of fields that are in POJO . and during the run of application the number of fields should change

Can any one suggest me what should i do


Solution

  • I don't know if this is good solution but here is what i did . If you are not using Morphia for Mongodb . and you want you POJO entity to be serialized . you can do this like code show below . but you have to maintain the setters call sequence if you want the data to be organized below are two class which gives some hint

    package com.mongodbtest.entities;
    
     import java.io.Serializable;
    
     import org.bson.Document;
    
     import org.bson.BsonDocument;
     import org.bson.BsonInt32;
      import org.bson.BsonNumber;
     import org.bson.BsonString;
    
    public class User extends BsonDocument {
    
    /**
         * 
         */
    private static final long serialVersionUID = -9167404209850672507L;
    // private String _id;
    private BsonString name;
    private BsonString email;
    private BsonString address;
    private BsonInt32 phno;
    
    public User(String name, String email, StringBuilder address, Number phno) {
        super();
        this.name = new BsonString(name);
        this.email = new BsonString(email);
        this.address = new BsonString(address.toString());
        this.phno = new BsonInt32(phno.intValue());
    }
    
    public User() {
    }
    
    public BsonString getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = new BsonString(name);
        this.append("name", this.name);
    
    }
    
    public BsonString getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = new BsonString(email);
        this.append("email", this.email);
    }
    
    public BsonString getAddress() {
        return address;
    }
    
    public void setAddress(StringBuilder address) {
        this.address = new BsonString(address.toString());
        this.append("address", this.address);
    }
    
    public BsonInt32 getPhno() {
        return phno;
    }
    
    public void setPhno(Number phno) {
        this.phno = new BsonInt32(phno.intValue());
        this.append("phno", this.phno);
    }
    
    }
    

    and Class that serializes this will be like this

    package com.mongodb.sample;
    
    import org.bson.BsonDocument;
    import org.bson.Document;
    
    import com.mogodbsample.config.DBconfig;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoCursor;
    import com.mongodb.client.MongoDatabase;
    import com.mongodbtest.entities.User;
    
    public class Firstmongoaccess {
    
    public static void main(String... args) {
        MongoDatabase dataase = DBconfig.getdatabase();
        MongoCollection<User> collection = dataase.getCollection("users",  User.class);
    
        User u1 = new User();
        u1.setAddress(new StringBuilder("something street bangalore 356322"));
        u1.setName("xxxxxx");
        u1.setEmail("[email protected]");
        u1.setPhno(3263433);
    
        collection.insertOne(u1);
        MongoCursor<? extends BsonDocument> cursor = collection.find().iterator();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }
    
    }
    }