2015-06-18 40 views
0

有一個名爲Accessdemo的類。它具有名稱,地址,電話號碼,電子郵件作爲其數據成員。我已經完成了與mongodb的連接,它正在工作。現在,我想將對象存儲在數據庫中並將其回收。任何解決方案,而不是提示我該如何做到這一點?如何使用java在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 = "[email protected]"; 
    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("[email protected]"); 
    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); 

    } 

} 

回答

0

您canuse ReflectionDBObject代替BasicDBObject或者你可以去一個Java ORM Morphia

+0

什麼是ORM嗎啡我不是理解它。它變得很難理解 –

1

以下是在插入和檢索對象使用文檔對象的示例:

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驅動程序3.0.2

欲瞭解更多信息,你可以去官方文檔:https://docs.mongodb.org/getting-started/java/query/

相關問題