Sonal
Sonal

Reputation: 405

How to avoid $oid in response of get by id in mongodb with java?

When performing the following code

    JSONObject jSONObject = new JSONObject();
        MongoClient mongo = new MongoClient(mongoip, mongoport);
            DB db = mongo.getDB(mongodb);

            DBCollection collection = db.getCollection("abc");
            query = new BasicDBObject();
            query.put("_id", new ObjectId(recordID));
            dbObj = collection.findOne(query);

            JSONObject output = new JSONObject(JSON.serialize(dbObj));

                                        jSONObject.put("data", output);
                res = jSONObject.toString();
    return res;

I am getting this response:

data {
  "_id":{"$oid":"5c90bc4c32ef32622181d86b"},
  "created_at":"2019-03-19 03:24:20",
  "updated_at":"2019-03-19 03:33:40",
  "Temp":"30",
  "Oil":"60",
  "test":"123"
}

But i don't want $oid in response, I want response like this:

data {
  "_id":"5c90bc4c32ef32622181d86b",
  "created_at":"2019-03-19 03:24:20",
  "updated_at":"2019-03-19 03:33:40",
  "Temp":"30",
  "Oil":"60",
  "test":"123"
}

Please help me getting response like this. What should i do?

Upvotes: 1

Views: 352

Answers (0)

Related Questions