Search code examples
javamongodbmongodb-querymongo-javamongo-java-driver

How to retrieve a value from mongoDB?


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": "*"
   }   
}

Solution

  • Try keySet() method.

    BasicDBObject searchQuery = new BasicDBObject();
    DBCursor cursor = table.find(searchQuery);
    
    while (cursor.hasNext()) {
        System.out.println(cursor.next().keySet());
    }
    

    check the docs