代码之家  ›  专栏  ›  技术社区  ›  Martin AJ

如何将db.collection查询转换为JSON查询?

  •  0
  • Martin AJ  · 技术社区  · 1 年前

    我有以下查询,它在MONGOSH上执行得很好:

    db.getCollection("user_products").aggregate( [
       {
           $lookup:
           {
             from: "users",
             localField: "user_id",
             foreignField: "_id",
             as: "user"
           }
       },
       {
           $lookup:
           {
             from: "products",
             localField: "product_id",
             foreignField: "_id",
             as: "product"
           }
       },
        {
            $unwind: "$product"
        },
        {
            $unwind: "$user"
        },
        {
            "$project": {
                "_id": 1,
                "creation_date": 1,
                "support_all_payment": 1,
                "payment": 1,
                "imported": 1,
                
                "name": "$user.name",
                "country_code": "$user.country_code",
                "phone_number": "$user.phone_number",
                "wallet_credit": "$user.wallet_credit",
                "score": "$user.score",
                "birthday": "$user.birthday",
                "bio": "$user.bio",
                "education": "$user.education",
                "email": "$user.email",
                "job": "$user.job",
                "major": "$user.major",
                "sex": "$user.sex",
                "have_you_attended_courses_on_personal_and_organizational_success": "$user.have_you_attended_courses_on_personal_and_organizational_success",
                "what_books_have_you_read_about_growth_and_development_and_success": "$user.what_books_have_you_read_about_growth_and_development_and_success",
                "which_of_the_success_professors_are_you_interested_in_and_what_is_the_reason_for_your_interest": "$user.which_of_the_success_professors_are_you_interested_in_and_what_is_the_reason_for_your_interest",
                "city": "$user.city",
                "state": "$user.state",
                "favorites": "$user.favorites",
                "fixed_number": "$user.fixed_number",
                
                
                "show": "$product.show",
                "title": "$product.title",
                "type": "$product.type",
                "little_description": "$product.little_description",
                "description": "$product.description",
                "price": "$product.price",
                "support_price": "$product.support_price",
                "discount": "$product.discount",
                           
             }
        }
    ] )
    

    由于我需要导出结果,并且我使用MongoDB Compass,所以我需要将上面的查询转换为JSON格式 {}

    我厌倦了一些在线工具,但没有成功,知道如何将其转换为JSON吗?

    0 回复  |  直到 1 年前
        1
  •  1
  •   Buzz Moschetti    1 年前

    如果该查询在中有效 mongosh 而你正在使用 蒙哥什语 那么您不需要Compass将输出导出为JSON。假设您的查询位于名为的文件中 q1.js ,大致如下:

    cursor = db.getCollection("user_products").aggregate([ ... ]}
    

    那就打电话 JSON.stringify() 在输出上发出正确的JSON:

    cursor.forEach(function(doc) {
        print(JSON.stringify(doc));
    });
    

    或者,为了保持类型保真度(以牺牲更复杂的输出结构为代价),请使用EJSON:

        print(EJSON.stringify(doc));
    

    并从命令行运行查询:

    $  mongosh --quiet mongodb://whereever/dbName q1.js > theOutput.json