2014년 12월 9일 화요일

how to get all the parents and children of a document

I recently started working on mongodb. My simple application will have Geo hierarchy like

Continet-->Country-->Region--->Resort-->Accommodation

My data will be like this

     {
        "_id" : ObjectId("54854d5af2ef42fe7d4d2a44"),
        "name" : "Europe",
        "parent" : "",
        "type" : "Continent",
        "synonym" : "",
        "code" : "EEE"
    }
    {
        "_id" : ObjectId("54854d5af2ef42fe7d4d2a47"),
        "name" : "Bulgaria",
        "parent" : "EEE",
        "type" : "Country",
        "synonym" : "",
        "code" : "BGR"
    }
    {
        "_id" : ObjectId("54854d5af2ef42fe7d4d2a45"),
        "name" : "Albania",
        "parent" : "EEE",
        "type" : "Country",
        "synonym" : "",
        "code" : "ALB"
    }
    {
        "_id" : ObjectId("54854d5af2ef42fe7d4d2a48"),
        "name" : "Bourgas Region",
        "parent" : "BGR",
        "type" : "Region",
        "synonym" : "",
        "code" : "002682"
    }
    {
        "_id" : ObjectId("54854d5af2ef42fe7d4d2a49"),
        "name" : "Obzor",
        "parent" : "002682",
        "type" : "Resort",
        "synonym" : "",
        "code" : "002911"
    }
    {
        "_id" : ObjectId("54854d5af2ef42fe7d4d2a4a"),
        "name" : "Sunny Beach",
        "parent" : "002682",
        "type" : "Resort",
        "synonym" : "",
        "code" : "002548"
    }
    {
        "_id" : ObjectId("54854d5af2ef42fa7d4d2b3d"),
        "name" : "Hotel Park Avenue",
        "parent" : "002548",
        "type" : "Accommodation",
        "synonym" : "",
        "code" : "022548"
    }

As shown above there will be only the direct parent available in every document.

Now I have to get all the parents and children to a document with  "code" : "002548"

If I have the Resort code, i have to get all the Accommodations which have the respective resort as parent and i have to get the Destination, Country, Continent to which the resort belongs to.

I feel that there should be recursive queries to get this, I am using MongoTemplate for db operations.

Please Help me to get the best solution for this.



Take a look at these examples that discuss how to efficiently store a tree type hierarchy in MongoDB:


Usually the method you pick will be dictated by the access pattern of your application (the types of reads and writes that you will make on the documents).


댓글 없음:

댓글 쓰기