菜单
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

MongoDB 聚合 $out


聚合 $out

此聚合阶段将聚合管道返回的文档写入一个集合。

The $out stage must be the last stage of the aggregation pipeline. ($out 阶段必须是聚合管道的最后一个阶段。)

示例

In this example, we are using the "sample_airbnb" database loaded from our sample data in the Intro to Aggregations section. (在此示例中,我们使用的是在“聚合入门”部分的示例数据中加载的“sample_airbnb”数据库。)

db.listingsAndReviews.aggregate([
  {
    $group: {
      _id: "$property_type",
      properties: {
        $push: {
          name: "$name",
          accommodates: "$accommodates",
          price: "$price",
        },
      },
    },
  },
  { $out: "properties_by_type" },
])
自己动手试一试 »

The first stage will group properties by the property_type and include the name, accommodates, and price fields for each. The $out stage will create a new collection called properties_by_type in the current database and write the resulting documents into that collection. (第一个阶段将按 property_type 对房产进行分组,并为每个房产包含 nameaccommodatesprice 字段。$out 阶段将在当前数据库中创建一个名为 properties_by_type 的新集合,并将结果文档写入该集合。)


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持