简体   繁体   中英

how to store objects in mongodb using java

There is a class called Accessdemo.It has name,address,phone number,email as its data members. I have done connection to mongodb and it is working. Now i wanna store object in the databse and retrive it. Any solution rather hint on how can i do this???

here is my code

package mongotutdemo;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

 class AccessObject extends BasicDBObject {
    public static final String COLLECTION_NAME = "Employee";
    String address = "";
    String First_Name = "Jeff";
    String last_Name ="Herrick";
    String email = "co@cpm.siemens.com";
    String phone_number="262-555-2724";

    public void setAddre(String value){
        address = value;
    }
    public String getAddress(){
        return address;
    }
    public void setFirstName(String value){
        First_Name = value;
    }
    public String getFName(){
        return First_Name;
    }
    public void setLName(String value){
        last_Name = value;
    }
    public String getLName(){
        return last_Name;
    }
    public void setEmail(String value){
        email = value;
    }
    public String getEmail(){
        return email;
    }
    public void setPNumber(String value){
        phone_number = value;
    }
    public String getPNumber(){
        return phone_number;
    }
}

public class AccessObjectID{

    public static void main(String[] args)throws UnknownHostException {

    AccessObject obj1 = new AccessObject();
    AccessObject obj2 = new AccessObject();
    AccessObject obj3 = new AccessObject();

    obj1.setAddre("Sector No:-42,Los Angeles,USA");
    obj1.setFirstName("Jack");
    obj1.setLName("Reacher");
    obj1.setEmail("Jack_reacher@myid.com");
    obj1.setPNumber("02024568963");


    MongoClient mongoclient = new MongoClient("localhost",27017);
    DB dbobj = mongoclient.getDB("mongotutdb");
    DBCollection colc = dbobj.getCollection(AccessObject.COLLECTION_NAME);

    colc.save(obj1);
    System.out.println("*******"+colc.findOne());


    BasicDBObject basicdbobj = new BasicDBObject();                                 

    //BasicDBObject basicdbobj1 = new BasicDBObject("name", "movie");

    for(int i=0;i<5;i++){
        basicdbobj.put("address", obj1.address);
    }
    //System.out.println("Object1 :-"+basicdbobj1);

    }

}

Following is an example of using Document object in inserting and retrieving objects:

    Document doc = new Document("_id", 99999);//here I initialised a Document object and directly put some data on it.
    doc.append("name", "My Name");//adding more data
    ArrayList<Document> scores = new ArrayList<Document>();//What if I want to insert an array of documents
    scores.add(new Document("score", 90).append("type", "exam"));//initialise and add a document to the array
    scores.add(new Document("score", 100).append("type", "homework"));//one more document added

    doc.append("scores", scores);//add the array to the root document
    coll.insertOne(doc);//Insert the root document into the collection


    //What if I want to retrieve this document
    Bson filter = new Document("_id",99999);//Retrieve it using its _id
    Document result = coll.find(filter).first();//Here will find the first result
    // What if I want to retrieve  multiple documents
    // ArrayList<Document> results = coll.find(filter).into(new ArrayList<Document>());

The other way to build your query, by using helper classes eg Filter or Sort , Projection.

Note: I am using Mongodb java driver 3.0.2

For more information , you can go to the official documentation : https://docs.mongodb.org/getting-started/java/query/

您可以使用ReflectionDBObject而不是BasicDBObject,或者可以使用Java ORM Morphia

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM