You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将MongoDB Shell聚合查询转换为JSON格式用于Compass导出?

转换MongoDB Shell聚合查询为Compass兼容的JSON格式

要将你提供的mongosh聚合查询转为MongoDB Compass可用的标准JSON,只需以下几步:

核心转换步骤

  • 提取管道数组:移除mongosh代码中的db.getCollection("user_products").aggregate(和末尾的),只保留括号内的聚合管道数组。
  • 修复转义引号:原代码中的"是HTML转义的双引号,全部替换为普通双引号"。
  • 确保JSON合规:所有键名必须用双引号包裹,数组和对象结构保持完整,无需额外JS语法(比如分号)。

转换后的完整JSON查询

[
   {
       "$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"
         }
    }
]

在Compass中使用的方法

  1. 打开MongoDB Compass,连接到目标数据库,进入user_products集合。
  2. 切换到聚合标签页。
  3. 点击导入管道,选择粘贴JSON,将上面的JSON代码粘贴进去,点击应用即可执行查询,之后就能导出结果。

为什么在线转换工具失败?

你提供的原始代码包含HTML转义字符",而非标准的JSON/JS引号,大多数在线工具无法自动识别并处理这种转义,导致转换失败。手动替换转义引号后即可正常使用。

内容的提问来源于stack exchange,提问作者Martin AJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 07:30:34