【问题标题】:Generate one json from find in 2 collections in mongodb从 mongodb 的 2 个集合中的 find 生成一个 json
【发布时间】:2013-08-29 15:39:29
【问题描述】:

你能帮我从两个集合中生成一个 json:

用户 [user_id, user_name, city_id]

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

城市 [ city_id, city_name ]

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

JSON 像这样:

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

【问题讨论】:

    标签: javascript json node.js mongodb


    【解决方案1】:

    您需要在应用层中进行连接,无法在 MongoDB 中连接数据。每当您在两个集合中有数据时,您都必须发出两个查询。

    【讨论】:

    • 我同意两个查询,但是,它如何在 nodejs 中正确写入?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多