如何使用java在mongodb中存储对象

时间:2015-06-18 07:23:35

标签: java mongodb

有一个名为Accessdemo的类。它有名称,地址,电话号码,电子邮件作为其数据成员。我已经完成了mongodb的连接,它正在工作。现在我想在数据库中存储对象并进行检索。任何解决方案相当暗示我怎么能这样做???

这是我的代码

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

    }

}

2 个答案:

答案 0 :(得分:2)

以下是在插入和检索对象时使用Document对象的示例:

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

使用辅助类构建查询的另一种方法,例如过滤或排序,投影。

注意:我使用的是Mongodb java driver 3.0.2

有关详细信息,请访问官方文档:https://docs.mongodb.org/getting-started/java/query/

答案 1 :(得分:0)

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