i could not able to retrieve the dbreference value from mongodb
// here i can add mapping code the clientinfo is parent table and jobrole is a child table
BasicDBObject obj=new BasicDBObject();
obj.put("phoneno", jobRole.getPhoneNo());
obj.put("jobrole", jobRole.getJobRole());
DBRef dbRef = new DBRef("ClientInfo", clientid);
obj.put("ClientInfo", dbRef);
return obj;
// here i can add code for fetching data from mongodb
BasicDBObject query1 = new BasicDBObject();
query1.put("jobrole.$clientid", "0c2a3c35-5b24-462d-99ee-eee28e1112d7");
query1.put("ClientInfo.clientid", "0c2a3c35-5b24-462d-99ee-eee28e1112d7");
DBObject obj = collection.findOne(query1);
System.out.println(obj.toMap());
// mongo config file: package com.aibots.qrcodeSystem.mongoConfig;
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class MongoConfigUtil {
private static MongoClient client=getClient();
public static MongoClient getClient()
{
if(client==null)
{
String host="localhost";
Integer port=27017;
String database="qrcode_system";
client=new MongoClient(host,port);
}
return client;
}
public static DB getDB(String dbName)
{
if(dbName !=null & !dbName.equals(""))
{
DB database=getClient().getDB(dbName);
return database;
}
return null;
}
public static DBCollection getCollection(String dbName, String collectionName) {
DB db=getDB(dbName);
if(db!=null)
{
DBCollection dbCollection=db.getCollection(collectionName);
return dbCollection;
}
return null;
}
public static DBObject findOne(String dbName, String collName, DBObject
query, DBObject fields) {
DBCollection collection = getCollection(dbName, collName);
if (collection != null) {
return collection.findOne(query, fields);
}
return null;
}
}
}