0

I am new to mongoDB and using java with mongoDB. I have a json where I want to retrieve the column names and not the value.I also need to store it in two different array.

Desired output is:

column [ ] = views, AddToCart, AddToWishList, ZoomedProductImage

list [ ] = fSymbol, num, operator

and the JSON:

{
    "views": {
        "fSymbol": "",
        "num": 0.1,
        "operator": "*"
    },
    "AddToCart": {
        "fSymbol": "+",
        "num": 0.15,
        "operator": "*"
   },
   "AddToWishList": {
       "fSymbol": "+", 
       "num": 0.1,
       "operator": "*"
   },
   "ZoomedProductImage": {
       "fSymbol": "+",
       "num": 0.07,
       "operator": "*"
   }   
}
Ritesh Sinha
  • 820
  • 5
  • 22
  • 50

2 Answers2

3

Try keySet() method.

BasicDBObject searchQuery = new BasicDBObject();
DBCursor cursor = table.find(searchQuery);

while (cursor.hasNext()) {
    System.out.println(cursor.next().keySet());
}

check the docs

singhakash
  • 7,891
  • 6
  • 31
  • 65
1

Since BasicDBObject extends HashMap, you have just to get the keyset from it.

Enrichman
  • 11,157
  • 11
  • 67
  • 101