blue-sky
blue-sky

Reputation: 53866

Remove [ "" : from mongoDb result using Java

To remove the "_id" from the mongo result I use :

DBObject allQuery = new BasicDBObject();
DBObject removeIdProjection = new BasicDBObject("_id", 0);
data.addAll(collection.find(allQuery , removeIdProjection).toArray());

The results of this query is :

    { "" : { [ 

    {
    "test1" : "test1"
    {
   }]}

How to remove { "" : so result is of format :

   [ 
    {
    "test1" : "test1"
    }
    ]   

Upvotes: 0

Views: 306

Answers (1)

Vishwas
Vishwas

Reputation: 7067

You are trying to put result in json object which add extra brackets here.

toArray() converts type cursor to list so you need to store it in list. You can iterate this list to access elements. You should use following code to get expected result:

DBObject allQuery = new BasicDBObject();
DBObject removeIdProjection = new BasicDBObject("_id", 0);
List cursor = collection.find(allQuery , emoveIdProjection).toArray();
System.out.println("result: " + cursor);

Upvotes: 1

Related Questions