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
  • 790
  • 3
  • 20
  • 47

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,773
  • 5
  • 28
  • 61
1

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

Enrichman
  • 10,917
  • 11
  • 68
  • 97