Http data api, how do I insert a item inside a nested objects

let’s say I want to insert a new cafe shop inside data array for “type”: “cafes”, how do I go about it? I’ve tried in postman but im struggling with it.

{
    "document": {
        "_id": "655ff93415a269f74e244a0f",
        "countries": [
            {
                "name": "United Kingdom",
                "cities": [
                    {
                        "name": "Liverpool",
                        "businesses": [
                            {
                                "id": "001",
                                "type": "cafes",
                                "data": [
                                    {
                                        "_id": "65601ba957e7c205da0f753a",
                                        "name": "Kimos Cafe",
                                        "address": "6 Myrtle St, Liverpool L7 7DP",
                                        "serviceOptions": "Dine-in, Takeaway",
                                        "phone": "0151 709 5392",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.400900",
                                            "lng": "-2.966640"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipPUqAKIoO3aFeNofEcA6mAYK545wCbw2Lxy2paT=s680-w680-h510"
                                    },
                                    {
                                        "_id": "65601bb057e7c205da0f753b",
                                        "name": "The Coffee Lodge",
                                        "address": "57 Lodge Lane, Liverpool L8 0QE",
                                        "serviceOptions": "Dine-in, Takeaway",
                                        "phone": "0151 280 4652",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "10 am",
                                                "close": "6 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.395760",
                                            "lng": "-2.949520"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipON0Ov0CxkyMO5xn7BQKLp8LCxGa68iZu8FuDEe=s680-w680-h510"
                                    },
                                    {
                                        "_id": "65601bb657e7c205da0f753c",
                                        "name": "Chaiiwala",
                                        "address": "5a Bold St, Liverpool L1 4DJ",
                                        "serviceOptions": "Dine-in, Takeaway, Delivery",
                                        "phone": "0151 709 0692",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "9 am",
                                                "close": "11 am"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.404590",
                                            "lng": "-2.981140"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipPwHmJkTbYORhJkAXYDRerY_OfuG87tJR7sEdpo=s680-w680-h510"
                                    }
                                ]
                            },
                            {
                                "id": "002",
                                "type": "butchers",
                                "data": [
                                    {
                                        "_id": "65601bc157e7c205da0f753d",
                                        "name": "J & I International Stores",
                                        "address": "99 Mulgrave St, Liverpool L8 2TD",
                                        "serviceOptions": "",
                                        "phone": "0151 709 3289",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "10 am",
                                                "close": "6:30 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "",
                                                "close": ""
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.392500",
                                            "lng": "-2.959690"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipNL2pXe62hSVtUjSu_IH3NdXerW3STT47yRrQ1H=s680-w680-h510"
                                    }
                                ]
                            },
                            {
                                "id": "003",
                                "type": "restaurants",
                                "data": [
                                    {
                                        "_id": "65601bca57e7c205da0f753e",
                                        "name": "Toro's Steakhouse",
                                        "address": "49 Bold St, Liverpool L1 4EU",
                                        "serviceOptions": "Dine-in, Takeaway, Delivery",
                                        "phone": "0151 345 1079",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.404758",
                                            "lng": "-2.981220"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipMtWhkPXmkI_fJPwJ1djmIO1dtYzyyA527h6HQO=s680-w680-h510"
                                    }
                                ]
                            },
                            {
                                "id": "004",
                                "type": "fast food",
                                "data": [
                                    {
                                        "name": "Heskey's",
                                        "address": "627 Prescot Rd, Old Swan, Liverpool L13 5XA",
                                        "serviceOptions": "",
                                        "phone": "07444 578203",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "2 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "2 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "2 pm",
                                                "close": "11 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.404758",
                                            "lng": "-2.981220"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipOCU2DGpYiKqZkZN4E9gL-ZTy-z78BQKIiRyfOa=s680-w680-h510",
                                        "_id": "65641189d94fa5402fb0ca6d"
                                    },
                                    {
                                        "name": "Smash'D Bros",
                                        "address": "189 Rice Ln, Liverpool L9 1DG",
                                        "serviceOptions": "Kerbside pickup, No-contact delivery, No dine-in",
                                        "phone": "0151 792 9867",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.454800",
                                            "lng": "-2.961780"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipO5HcRL7SNxF9Qi5h1CgGvuqt-aIqkPqgqiMc89=s680-w680-h510",
                                        "_id": "6564df9bf638d79d68b840ba"
                                    },
                                    {
                                        "name": "Bubbas Trap Kitchen",
                                        "address": "7 Smithdown Rd, Liverpool L7 4JB",
                                        "serviceOptions": "Kerbside pickup, Delivery",
                                        "phone": "07523 169138",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.398350",
                                            "lng": "-2.949240"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipOPDZD1NZpEJl3K5XychaeSj_dTTPaPXHqak7Bu=s680-w680-h510",
                                        "_id": "656802953c819548cd7632eb"
                                    },
                                    {
                                        "name": "Kilos Trap Kitchen",
                                        "address": "33 Holt Rd, Liverpool L7 2PL",
                                        "serviceOptions": " Dine-in, Kerbside pickup, No-contact delivery",
                                        "phone": "0151 709 3219",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.410440",
                                            "lng": "-2.947740"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipPKGsFG6CAJ64xF42uKXJi2e8zYbkQqtiERvHTp=s680-w680-h510",
                                        "_id": "656807a33c819548cd7632ec"
                                    }
                                ]
                            }
                        ],
                        "_id": "65601a1557e7c205da0f7539"
                    }
                ]
            }
        ]
    }
}

Hi @Ismail_Mohamed ,

Welcome to the MongoDB Community.

Based on your statement, it looks like you want to update the existing document in data field where type = cafes. You’re trying to update it with Postman but you’re struggling with it.

You can make sure about these things in order for having successful update operation. In your case:

  1. Make sure you’ve an update endpoint on the application api side
  2. Your update API endpoint should have and update operation ie db.collection.updateMany or db.collection.updateOne
    Example:
    cafes = db.collection.findOne({"countries.cities.businesses.type": "cafes"})
    cafes.document.countries.cities.businesses.data = data
    db.collection.updateOne({"countries.cities.businesses.type": "cafes"}, {$set: cafes})
    Where data is an array which you can prepare and pass from the driver side that you’re using
  3. Call that API endpoint from postman with just that data object that you would want to update rather than whole document

This would achieve the results that you described for in your post.

Note: Looking at your data model, the updates that you’re trying to make will apply for all the cities having type as cafes with the data object that you will pass. Make sure that aligns with your requirements or use case.

Feel free to post any doubts that you shall have.

Happy coding!

Hi, I’m not using a driver, my mobile app uses http data API and I’m testing it on postman.

I want to insert a new item (shop) inside cafes array, how do I structure thebbody in postman. Json body

in postman, im using the /findOne endpoint,

here’s the body

{
    "collection": "",
    "database": "",
    "dataSource": "",
    "filter": {
        "countries.cities.businesses.type": "cafes"
    }
}

then my response is

{
    "document": {
        "_id": "655ff93415a269f74e244a0f",
        "countries": [
            {
                "name": "United Kingdom",
                "cities": [
                    {
                        "name": "Liverpool",
                        "businesses": [
                            {
                                "type": "cafes",
                                "data": [
                                    {
                                        "_id": "65601ba957e7c205da0f753a",
                                        "name": "Kimos Cafe",
                                        "address": "6 Myrtle St, Liverpool L7 7DP",
                                        "serviceOptions": "Dine-in, Takeaway",
                                        "phone": "0151 709 5392",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "10 am",
                                                "close": "11:30 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.400900",
                                            "lng": "-2.966640"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipPUqAKIoO3aFeNofEcA6mAYK545wCbw2Lxy2paT=s680-w680-h510"
                                    },
                                    {
                                        "_id": "65601bb057e7c205da0f753b",
                                        "name": "The Coffee Lodge",
                                        "address": "57 Lodge Lane, Liverpool L8 0QE",
                                        "serviceOptions": "Dine-in, Takeaway",
                                        "phone": "0151 280 4652",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "9 am",
                                                "close": "6 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "10 am",
                                                "close": "6 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.395760",
                                            "lng": "-2.949520"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipON0Ov0CxkyMO5xn7BQKLp8LCxGa68iZu8FuDEe=s680-w680-h510"
                                    },
                                    {
                                        "_id": "65601bb657e7c205da0f753c",
                                        "name": "Chaiiwala",
                                        "address": "5a Bold St, Liverpool L1 4DJ",
                                        "serviceOptions": "Dine-in, Takeaway, Delivery",
                                        "phone": "0151 709 0692",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "8 am",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "9 am",
                                                "close": "11 am"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.404590",
                                            "lng": "-2.981140"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipPwHmJkTbYORhJkAXYDRerY_OfuG87tJR7sEdpo=s680-w680-h510"
                                    }
                                ],
                                "_id": "656bd281681622977480b364"
                            },
                            {
                                "type": "butchers",
                                "data": [
                                    {
                                        "_id": "65601bc157e7c205da0f753d",
                                        "name": "J & I International Stores",
                                        "address": "99 Mulgrave St, Liverpool L8 2TD",
                                        "serviceOptions": "",
                                        "phone": "0151 709 3289",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "10 am",
                                                "close": "7:30 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "10 am",
                                                "close": "6:30 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "",
                                                "close": ""
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.392500",
                                            "lng": "-2.959690"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipNL2pXe62hSVtUjSu_IH3NdXerW3STT47yRrQ1H=s680-w680-h510"
                                    }
                                ],
                                "_id": "656bd1eb681622977480b360"
                            },
                            {
                                "type": "restaurants",
                                "data": [
                                    {
                                        "_id": "65601bca57e7c205da0f753e",
                                        "name": "Toro's Steakhouse",
                                        "address": "49 Bold St, Liverpool L1 4EU",
                                        "serviceOptions": "Dine-in, Takeaway, Delivery",
                                        "phone": "0151 345 1079",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "12 pm",
                                                "close": "10 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.404758",
                                            "lng": "-2.981220"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipMtWhkPXmkI_fJPwJ1djmIO1dtYzyyA527h6HQO=s680-w680-h510"
                                    }
                                ],
                                "_id": "656bd1fc681622977480b361"
                            },
                            {
                                "type": "fast food",
                                "data": [
                                    {
                                        "name": "Heskey's",
                                        "address": "627 Prescot Rd, Old Swan, Liverpool L13 5XA",
                                        "serviceOptions": "",
                                        "phone": "07444 578203",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "2 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "2 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "2 pm",
                                                "close": "11 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.404758",
                                            "lng": "-2.981220"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipOCU2DGpYiKqZkZN4E9gL-ZTy-z78BQKIiRyfOa=s680-w680-h510",
                                        "_id": "65641189d94fa5402fb0ca6d"
                                    },
                                    {
                                        "name": "Smash'D Bros",
                                        "address": "189 Rice Ln, Liverpool L9 1DG",
                                        "serviceOptions": "Kerbside pickup, No-contact delivery, No dine-in",
                                        "phone": "0151 792 9867",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "3:45 pm",
                                                "close": "11 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.454800",
                                            "lng": "-2.961780"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipO5HcRL7SNxF9Qi5h1CgGvuqt-aIqkPqgqiMc89=s680-w680-h510",
                                        "_id": "6564df9bf638d79d68b840ba"
                                    },
                                    {
                                        "name": "Bubbas Trap Kitchen",
                                        "address": "7 Smithdown Rd, Liverpool L7 4JB",
                                        "serviceOptions": "Kerbside pickup, Delivery",
                                        "phone": "07523 169138",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "4 pm",
                                                "close": "11 pm"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.398350",
                                            "lng": "-2.949240"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipOPDZD1NZpEJl3K5XychaeSj_dTTPaPXHqak7Bu=s680-w680-h510",
                                        "_id": "656802953c819548cd7632eb"
                                    },
                                    {
                                        "name": "Kilos Trap Kitchen",
                                        "address": "33 Holt Rd, Liverpool L7 2PL",
                                        "serviceOptions": " Dine-in, Kerbside pickup, No-contact delivery",
                                        "phone": "0151 709 3219",
                                        "hours": [
                                            {
                                                "day": "Monday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Tuesday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Wednesday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Thursday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Friday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Saturday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            },
                                            {
                                                "day": "Sunday",
                                                "open": "4 pm",
                                                "close": "12 am"
                                            }
                                        ],
                                        "ratings": [
                                            {
                                                "userId": "",
                                                "rate": 0
                                            }
                                        ],
                                        "location": {
                                            "lat": "53.410440",
                                            "lng": "-2.947740"
                                        },
                                        "imageUrl": "https://lh3.googleusercontent.com/p/AF1QipPKGsFG6CAJ64xF42uKXJi2e8zYbkQqtiERvHTp=s680-w680-h510",
                                        "_id": "656807a33c819548cd7632ec"
                                    }
                                ],
                                "_id": "656bd200681622977480b362"
                            }
                        ],
                        "_id": "65601a1557e7c205da0f7539"
                    }
                ]
            }
        ]
    }
}

why is it returning all objects in businesses and not just a single object of cafes??

Hi Ismail,

If you’re doing findOne without projection, it will return whole document which matches with businesses.type = "cafes". If you want specific fields to be return, you will need to use field projection so that you will get field that you need in result.

Example, If you just want cafes data, This is something you can do in normal Mongo query.

db.collection.findOne({"countries.cities.businesses.type": "cafes"}, {"_id": 0, "countries.cities.businesses.type": 1, "countries.cities.businesses.data": 1})

Checkout this document to find out about projection and how you can pass it in findOne.

Now, since you’re using Atlas Data API, You can pass projection for fineOne like this:

{
    "collection": "",
    "database": "",
    "dataSource": "",
    "filter": {
        "countries.cities.businesses.type": "cafes"
    },
   "projection": {
        "_id": 0,
       "countries.cities.businesses.type": 1,
       "countries.cities.businesses.data": 1
   }
}

Checkout this document to figure out how you can use findOne with projection and other operators.

You will find insert and update request formats as well on the same link from the left menu.

Give it a go and let me know how it goes.

Feel free to post any issues or questions that you shall have.

Happy coding!

Cheers!

that didn’t work for me but I used endpoint/data/v1/action/aggregate instead, and it works fine

{
    "collection": "",
    "database": "",
    "dataSource": "",
    "pipeline": [
        {
            "$unwind": "$countries"
        },
        {
            "$unwind": "$countries.cities"
        },
        {
            "$unwind": "$countries.cities.businesses"
        },
        {
            "$match": {
                "countries.cities.businesses.type": "cafes"
            }
        },
        {
            "$group": {
                "_id": "$_id",
                "cafes": {
                    "$push": "$countries.cities.businesses.data"
                }
            }
        }
    ]
}

I just have one more question, how do I insert a new coffee shop item inside cafes

I believe you want to add coffee shop inside data field where type=cafe .You can use $push to add new coffee shop item inside cafes data field. Just make the appropriate match condition in update operation. Checkout this document to find out the correct way to do it.

I hope this answers to your query.

1 Like