简体   繁体   中英

Generate one json from find in 2 collections in mongodb

Can you help me to generate one json from two collections:

Users [ user_id, user_name, city_id ]

[
    {
        "name": "Anton",
        "user_id": 1,
        "city_id": 1
    },
    {
        "name": "Vasiliy",
        "user_id": 2,
        "city_id": 2
    }
]

Cities [ city_id, city_name ]

[
    {
        "name": "Kharkov",
        "city_id": 1
    },
    {
        "name": "Kiev",
        "city_id": 2
    }
]

JSON Like this:

[
        {
            "user_name": "Anton",
            "city_name": "Kharkov",
            "user_id": 1,
        },
        {
            "user_name": "Vasiliy",
            "city_name": "Kiev",
            "user_id": 2,
        }
]

You'll need to do the join in your application layer, there is no way to join the data in MongoDB. Any time you have data in two collections you will have to issue two queries.

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