简体   繁体   中英

Accessing nested data in MongoDB through REST, Spring

Akin to my earlier question , I'm trying to access data in MongoDB using Spring REST.

I have collections of simple Key-Value Pairs and can access those fine.

{
    "_id" : ObjectId("5874ab4a19b38fb91fbb234f"),
    "roID" : "7ed3f9a6-bb9b-4d16-8d1a-001b7ec40b51",
    "Name" : "[REDACTED]"
}

The problem is, these objects are used in another collection that displays a relationship with properties between them, like this:

{
    "_id" : ObjectId("5874ab4f19b38fb91fbb6180"),
    "[OBJECT CATEGORY A]" : {
        "_id" : ObjectId("5874ab4a19b38fb91fbb257b"),
        "roID" : "72f8a8b5-71a7-40ac-b1ac-1ffc98a507ba",
        "Name" : "[REDACTED]"
    },
    "[OBJECT CATEGORY B]" : {
        "_id" : ObjectId("5874ab4b19b38fb91fbb32a3"),
        "roID" : "919446ab-1898-419f-a704-e8c34985f945",
        "Name" : "[REDACTED]"
    },
    "[RELATIONSHIP INFORMATION]" : [ 
        {
            "[PROPERTY A]" : [ 
                {
                    "[VALUE A]" : 5.0
                }, 
                {
                    "[VALUE B]" : 0.0
                }
            ]
        }, 

Properties are somewhere between 8 and 20.

The definition of the first (plain) object in Java looks like this:

@Document(collection="OBJ")
public class Obj {

    public Obj(){};

    @Id
    public String id;

    @Field("roID")
    public String roID;

    @Field("Name")
    public String name;

}

The repository class:

@RepositoryRestResource(collectionResourceRel = "OBJ", path = "OBJ")
public interface ObjRepo extends MongoRepository<Obj, String> {

    List<Obj> findByName(@Param("name") String name); 
}

The question is: how do I access the nested objects? I've tried using LinkedHashMap in place of the Strings for the complex collection, curl only returns "null" when I try to access them. I tried defining a class

public class BITS {
    @Id
    private String _id;
    @Field("roID")
    private String roID;
    @Field("Name")
    private String name;

    public BITS(){}

    public BITS(String _id,String roID, String name){
        this._id = _id;
        this.roID = roID;
        this.name = name;
    }

}

to access these objects, unsuccessfully.

Turns out the class approach was correct, just not well executed. I've created a plain JSON Collection for testing purposes:

@Document(collection="JSON")
public class JSON {

    @Id
    public String id;

    @Field("squares")
    public Square square;

    @Field("dots")
    public Dot dot;

    public JSON(){};
    public JSON(String id, Square square,Dot dot){
        this.id = id;
        this.square = square;
        this.dot = dot;
    };


}

Square.java

public class Square {

    private String id;
    private int x;
    private int y;

    public Square(){};

    public Square(String id,int x, int y){
        this.id = id;
        this.x = x;
        this.y = y;
    };

    public Map<String, Integer> getSquare()
    {
    Map<String, Integer> res = new HashMap<>();
    res.put("x", x);
    res.put("y", y);
    return res;
}

}

(Dot is the same, just for the test) So it's just about remodeling the desired response exactly, despite it being in that format in the database already. If anyone could point me to where I can remove the clutter from the response tho, that would be nice. Currently looks like this:

"_embedded" : {
    "JSON" : [ {
      "square" : null,
      "dot" : {
        "dot" : {
          "x" : 4,
          "y" : 3
        }
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/JSON/58ac466160fb39e5e8dc8b70"
        },
        "jSON" : {
          "href" : "http://localhost:8080/JSON/58ac466160fb39e5e8dc8b70"
        }
      }
    }, {
      "square" : {
        "square" : {
          "x" : 12,
          "y" : 2
        }
      },
      "dot" : null,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/JSON/58ac468060fb39e5e8dc8b7e"
        },
        "jSON" : {
          "href" : "http://localhost:8080/JSON/58ac468060fb39e5e8dc8b7e"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/JSON"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/JSON"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM