Mongoose subdocuments vs nested schema

后端 未结 6 1000
萌比男神i
萌比男神i 2020-11-29 17:59

I\'m curious as to the pros and cons of using subdocuments vs a deeper layer in my main schema:

var subDoc = new Schema({
  name: String
});

var mainDoc = n         


        
6条回答
  •  渐次进展
    2020-11-29 18:20

    Basically, create a variable nestedDov and put it here name: [nestedDov]

    Simple Version:

    var nestedDoc = new Schema({
      name: String
    });
    
    var mainDoc = new Schema({
      names: [nestedDoc]
    });
    

    JSON Example

    {
        "_id" : ObjectId("57c88bf5818e70007dc72e85"),
        "name" : "Corinthia Hotel Budapest",
        "stars" : 5,
        "description" : "The 5-star Corinthia Hotel Budapest on the Grand Boulevard offers free access to its Royal Spa",
        "photos" : [
            "/photos/hotel/corinthiahotelbudapest/1.jpg",
            "/photos/hotel/corinthiahotelbudapest/2.jpg"
        ],
        "currency" : "HUF",
        "rooms" : [
            {
                "type" : "Superior Double or Twin Room",
                "number" : 20,
                "description" : "These are some great rooms",
                "photos" : [
                    "/photos/room/corinthiahotelbudapest/2.jpg",
                    "/photos/room/corinthiahotelbudapest/5.jpg"
                ],
                "price" : 73000
            },
            {
                "type" : "Deluxe Double Room",
                "number" : 50,
                "description" : "These are amazing rooms",
                "photos" : [
                    "/photos/room/corinthiahotelbudapest/4.jpg",
                    "/photos/room/corinthiahotelbudapest/6.jpg"
                ],
                "price" : 92000
            },
            {
                "type" : "Executive Double Room",
                "number" : 25,
                "description" : "These are amazing rooms",
                "photos" : [
                    "/photos/room/corinthiahotelbudapest/4.jpg",
                    "/photos/room/corinthiahotelbudapest/6.jpg"
                ],
                "price" : 112000
            }
        ],
        "reviews" : [
            {
                "name" : "Tamas",
                "id" : "/user/tamas.json",
                "review" : "Great hotel",
                "rating" : 4
            }
        ],
        "services" : [
            "Room service",
            "Airport shuttle (surcharge)",
            "24-hour front desk",
            "Currency exchange",
            "Tour desk"
        ]
    }
    

    Example:

提交回复
热议问题