0

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;
    }
    }
    }
5
  • Is there any error or exception. Aug 15, 2019 at 4:21
  • getting null pointer exception Aug 15, 2019 at 4:35
  • Add the stacktrace to the question Aug 15, 2019 at 4:36
  • Exception in thread "main" java.lang.NullPointerException Aug 15, 2019 at 4:40
  • click edit and add the full stacktrace to the question. Aug 15, 2019 at 4:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.