gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:35:14 24 4
gpt4 key购买 nike

有一个名为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);

}

}

最佳答案

以下是使用 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 驱动程序 3.0.2

更多信息可以查看官方文档:https://docs.mongodb.org/getting-started/java/query/

关于java - 如何使用java在mongodb中存储对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30908979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com