Mongodb 获取计数和文档详细信息

Mongodb 获取计数和文档详细信息,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有一个这样的收藏: [{ "_id": "5ba6b67ab22f62939eba24cc", "voucher": "77-SRNP-4", "Collection Date": "1977-06-06T06:00:00.000Z", "Herbivore species": "Agrius cingulata", "Herbivore subfamily": "Sphinginae", "Latitude": "10.83764",

我有一个这样的收藏:

[{
    "_id": "5ba6b67ab22f62939eba24cc",
    "voucher": "77-SRNP-4",
    "Collection Date": "1977-06-06T06:00:00.000Z",
    "Herbivore species": "Agrius cingulata",
    "Herbivore subfamily": "Sphinginae",
    "Latitude": "10.83764",
    "Longitude": "-85.61871"
}, {
    "_id": "5ba6b67ab22f62939eba24ea",
    "voucher": "78-SRNP-10",
    "Collection Date": "1978-05-20T06:00:00.000Z",
    "Herbivore species": "Xylophanes turbata",
    "Herbivore subfamily": "Macroglossinae",
    "Latitude": "10.80212",
    "Longitude": "-85.65372"
}, {
    "_id": "5ba6b67ab22f62939eba24eb",
    "voucher": "78-SRNP-10.02",
    "Collection Date": "1978-05-20T06:00:00.000Z",
    "Herbivore species": "Xylophanes turbata",
    "Herbivore subfamily": "Macroglossinae",
    "Latitude": "10.80212",
    "Longitude": "-85.65372"
}]
db.collection.aggregate([
    {
        $group: {
            _id: "$Herbivore species",
            records: { $push: { Longitude: "$Longitude", Latitude: "$Latitude", "Collection Date": "$Collection Date", voucher: "$voucher" } },
            count : { $sum :1}
        }
    }
])
我想在一次查询中得到物种的数量以及每个记录的一些细节。类似于
$unwind
的oposite。要获得类似于:

[{
    "Agrius cingulata": {
        count: 1,
        "Herbivore subfamily": "Sphinginae"
        records: [{
            "voucher": "77-SRNP-4",
            "Collection Date": "1977-06-06T06:00:00.000Z",
            "Latitude": "10.83764",
            "Longitude": "-85.61871"
        }]

    },
    "Xylophanes turbata": {
        count: 2,
        "Herbivore subfamily": "Macroglossinae",
        records: [
            {
                "voucher": "78-SRNP-10",
                "Collection Date": "1978-05-20T06:00:00.000Z",
                "Latitude": "10.80212",
                "Longitude": "-85.65372"
            },
            {
                "voucher": "78-SRNP-10.02",
                "Collection Date": "1978-05-20T06:00:00.000Z",
                "Latitude": "10.80212",
                "Longitude": "-85.65372"
            }
        ]
    }
}]
我目前正在处理两个独立的查询,一个用于查找记录,另一个用于计数。然而,有效载荷有点大,我想如果我只发送一次重复的信息,就像物种亚科一样,并将计数和其他统计数据捆绑起来,我可以减少它,但我没有找到适当的聚合

谢谢

试试这个:

[{
    "_id": "5ba6b67ab22f62939eba24cc",
    "voucher": "77-SRNP-4",
    "Collection Date": "1977-06-06T06:00:00.000Z",
    "Herbivore species": "Agrius cingulata",
    "Herbivore subfamily": "Sphinginae",
    "Latitude": "10.83764",
    "Longitude": "-85.61871"
}, {
    "_id": "5ba6b67ab22f62939eba24ea",
    "voucher": "78-SRNP-10",
    "Collection Date": "1978-05-20T06:00:00.000Z",
    "Herbivore species": "Xylophanes turbata",
    "Herbivore subfamily": "Macroglossinae",
    "Latitude": "10.80212",
    "Longitude": "-85.65372"
}, {
    "_id": "5ba6b67ab22f62939eba24eb",
    "voucher": "78-SRNP-10.02",
    "Collection Date": "1978-05-20T06:00:00.000Z",
    "Herbivore species": "Xylophanes turbata",
    "Herbivore subfamily": "Macroglossinae",
    "Latitude": "10.80212",
    "Longitude": "-85.65372"
}]
db.collection.aggregate([
    {
        $group: {
            _id: "$Herbivore species",
            records: { $push: { Longitude: "$Longitude", Latitude: "$Latitude", "Collection Date": "$Collection Date", voucher: "$voucher" } },
            count : { $sum :1}
        }
    }
])
结果:

{
    "_id" : "Xylophanes turbata",
    "records" : [
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10"
        },
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10.02"
        }
    ],
    "count" : 2
},

/* 2 */
{
    "_id" : "Agrius cingulata",
    "records" : [
        {
            "Longitude" : "-85.61871",
            "Latitude" : "10.83764",
            "Collection Date" : "1977-06-06T06:00:00.000Z",
            "voucher" : "77-SRNP-4"
        }
    ],
    "count" : 1
}
要对“食草动物物种”和“食草动物亚科”进行分类,您可以尝试以下方法:

db.collection.aggregate([
    {
        $group: {
            _id: { "Herbivore species" :"$Herbivore species" , "Herbivore subfamily": "$Herbivore subfamily" },
            records: { $push: { Longitude: "$Longitude", Latitude: "$Latitude", "Collection Date": "$Collection Date", voucher: "$voucher" } },
            count : { $sum :1}
        }
    }
])
{
    "_id" : {
        "Herbivore species" : "Xylophanes turbata",
        "Herbivore subfamily" : "Macroglossinae"
    },
    "records" : [
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10"
        },
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10.02"
        }
    ],
    "count" : 2
},

/* 2 */
{
    "_id" : {
        "Herbivore species" : "Agrius cingulata",
        "Herbivore subfamily" : "Sphinginae"
    },
    "records" : [
        {
            "Longitude" : "-85.61871",
            "Latitude" : "10.83764",
            "Collection Date" : "1977-06-06T06:00:00.000Z",
            "voucher" : "77-SRNP-4"
        }
    ],
    "count" : 1
}
结果如下:

db.collection.aggregate([
    {
        $group: {
            _id: { "Herbivore species" :"$Herbivore species" , "Herbivore subfamily": "$Herbivore subfamily" },
            records: { $push: { Longitude: "$Longitude", Latitude: "$Latitude", "Collection Date": "$Collection Date", voucher: "$voucher" } },
            count : { $sum :1}
        }
    }
])
{
    "_id" : {
        "Herbivore species" : "Xylophanes turbata",
        "Herbivore subfamily" : "Macroglossinae"
    },
    "records" : [
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10"
        },
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10.02"
        }
    ],
    "count" : 2
},

/* 2 */
{
    "_id" : {
        "Herbivore species" : "Agrius cingulata",
        "Herbivore subfamily" : "Sphinginae"
    },
    "records" : [
        {
            "Longitude" : "-85.61871",
            "Latitude" : "10.83764",
            "Collection Date" : "1977-06-06T06:00:00.000Z",
            "voucher" : "77-SRNP-4"
        }
    ],
    "count" : 1
}
试试这个:

[{
    "_id": "5ba6b67ab22f62939eba24cc",
    "voucher": "77-SRNP-4",
    "Collection Date": "1977-06-06T06:00:00.000Z",
    "Herbivore species": "Agrius cingulata",
    "Herbivore subfamily": "Sphinginae",
    "Latitude": "10.83764",
    "Longitude": "-85.61871"
}, {
    "_id": "5ba6b67ab22f62939eba24ea",
    "voucher": "78-SRNP-10",
    "Collection Date": "1978-05-20T06:00:00.000Z",
    "Herbivore species": "Xylophanes turbata",
    "Herbivore subfamily": "Macroglossinae",
    "Latitude": "10.80212",
    "Longitude": "-85.65372"
}, {
    "_id": "5ba6b67ab22f62939eba24eb",
    "voucher": "78-SRNP-10.02",
    "Collection Date": "1978-05-20T06:00:00.000Z",
    "Herbivore species": "Xylophanes turbata",
    "Herbivore subfamily": "Macroglossinae",
    "Latitude": "10.80212",
    "Longitude": "-85.65372"
}]
db.collection.aggregate([
    {
        $group: {
            _id: "$Herbivore species",
            records: { $push: { Longitude: "$Longitude", Latitude: "$Latitude", "Collection Date": "$Collection Date", voucher: "$voucher" } },
            count : { $sum :1}
        }
    }
])
结果:

{
    "_id" : "Xylophanes turbata",
    "records" : [
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10"
        },
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10.02"
        }
    ],
    "count" : 2
},

/* 2 */
{
    "_id" : "Agrius cingulata",
    "records" : [
        {
            "Longitude" : "-85.61871",
            "Latitude" : "10.83764",
            "Collection Date" : "1977-06-06T06:00:00.000Z",
            "voucher" : "77-SRNP-4"
        }
    ],
    "count" : 1
}
要对“食草动物物种”和“食草动物亚科”进行分类,您可以尝试以下方法:

db.collection.aggregate([
    {
        $group: {
            _id: { "Herbivore species" :"$Herbivore species" , "Herbivore subfamily": "$Herbivore subfamily" },
            records: { $push: { Longitude: "$Longitude", Latitude: "$Latitude", "Collection Date": "$Collection Date", voucher: "$voucher" } },
            count : { $sum :1}
        }
    }
])
{
    "_id" : {
        "Herbivore species" : "Xylophanes turbata",
        "Herbivore subfamily" : "Macroglossinae"
    },
    "records" : [
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10"
        },
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10.02"
        }
    ],
    "count" : 2
},

/* 2 */
{
    "_id" : {
        "Herbivore species" : "Agrius cingulata",
        "Herbivore subfamily" : "Sphinginae"
    },
    "records" : [
        {
            "Longitude" : "-85.61871",
            "Latitude" : "10.83764",
            "Collection Date" : "1977-06-06T06:00:00.000Z",
            "voucher" : "77-SRNP-4"
        }
    ],
    "count" : 1
}
结果如下:

db.collection.aggregate([
    {
        $group: {
            _id: { "Herbivore species" :"$Herbivore species" , "Herbivore subfamily": "$Herbivore subfamily" },
            records: { $push: { Longitude: "$Longitude", Latitude: "$Latitude", "Collection Date": "$Collection Date", voucher: "$voucher" } },
            count : { $sum :1}
        }
    }
])
{
    "_id" : {
        "Herbivore species" : "Xylophanes turbata",
        "Herbivore subfamily" : "Macroglossinae"
    },
    "records" : [
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10"
        },
        {
            "Longitude" : "-85.65372",
            "Latitude" : "10.80212",
            "Collection Date" : "1978-05-20T06:00:00.000Z",
            "voucher" : "78-SRNP-10.02"
        }
    ],
    "count" : 2
},

/* 2 */
{
    "_id" : {
        "Herbivore species" : "Agrius cingulata",
        "Herbivore subfamily" : "Sphinginae"
    },
    "records" : [
        {
            "Longitude" : "-85.61871",
            "Latitude" : "10.83764",
            "Collection Date" : "1977-06-06T06:00:00.000Z",
            "voucher" : "77-SRNP-4"
        }
    ],
    "count" : 1
}
“反向”的基本概念当然是“反向”。因此,基本上这就是您要做的事情,在适当的地方,以及,因为不需要指定文档中的每个字段,特别是在文档中的字段实际上比问题中的字段多得多的地方,有一些额外的用法

以下内容相当通用,不关心文档中还有多少其他字段:

db.collection.aggregate([
  { "$group": {
    "_id": "$Herbivore species",
    "count": { "$sum": 1 },
    "Herbivore subfamily": { "$first": "$Herbivore subfamily" },
    "records": {
      "$push": {
        "$arrayToObject": {
          "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
            "cond": { "$not": { "$in": [ "$$this.k", ["Herbivore subfamily", "Herbivore species"] ] } }
          }
        }
      }
    }
  }}
])
这将产生如下结果:

{
        "_id" : "Agrius cingulata",
        "count" : 1,
        "Herbivore subfamily" : "Sphinginae",
        "records" : [
                {
                        "_id" : "5ba6b67ab22f62939eba24cc",
                        "voucher" : "77-SRNP-4",
                        "Collection Date" : "1977-06-06T06:00:00.000Z",
                        "Latitude" : "10.83764",
                        "Longitude" : "-85.61871"
                }
        ]
}
{
        "_id" : "Xylophanes turbata",
        "count" : 2,
        "Herbivore subfamily" : "Macroglossinae",
        "records" : [
                {
                        "_id" : "5ba6b67ab22f62939eba24ea",
                        "voucher" : "78-SRNP-10",
                        "Collection Date" : "1978-05-20T06:00:00.000Z",
                        "Latitude" : "10.80212",
                        "Longitude" : "-85.65372"
                },
                {
                        "_id" : "5ba6b67ab22f62939eba24eb",
                        "voucher" : "78-SRNP-10.02",
                        "Collection Date" : "1978-05-20T06:00:00.000Z",
                        "Latitude" : "10.80212",
                        "Longitude" : "-85.65372"
                }
        ]
}
不完全是问题中要求的,因为它当然没有以要求的方式准确显示结果的“关键点”。但这可以通过第二阶段进行修改,与之前相同的运营商展示:

db.collection.aggregate([
  { "$group": {
    "_id": "$Herbivore species",
    "count": { "$sum": 1 },
    "Herbivore subfamily": { "$first": "$Herbivore subfamily" },
    "records": {
      "$push": {
        "$arrayToObject": {
          "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
            "cond": { "$not": { "$in": [ "$$this.k", ["Herbivore subfamily", "Herbivore species"] ] } }
          }
        }
      }
    }
  }},
  { "$group": {
    "_id": null,
    "content": {
      "$mergeObjects": {
        "$arrayToObject": [[
          { "k": "$_id",
            "v": {
              "$arrayToObject": {
                "$filter": {
                  "input": { "$objectToArray": "$$ROOT" },
                  "cond": { "$ne": ["$$this.k", "_id"] }
                }
              }
            }
          }
        ]]
      }
    }
  }},
  { "$replaceRoot": { "newRoot": "$content" } }
])
返回:

{
        "Xylophanes turbata" : {
                "count" : 2,
                "Herbivore subfamily" : "Macroglossinae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24ea",
                                "voucher" : "78-SRNP-10",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        },
                        {
                                "_id" : "5ba6b67ab22f62939eba24eb",
                                "voucher" : "78-SRNP-10.02",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        }
                ]
        },
        "Agrius cingulata" : {
                "count" : 1,
                "Herbivore subfamily" : "Sphinginae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24cc",
                                "voucher" : "77-SRNP-4",
                                "Collection Date" : "1977-06-06T06:00:00.000Z",
                                "Latitude" : "10.83764",
                                "Longitude" : "-85.61871"
                        }
                ]
        }
}
或者,如果您愿意(因为它不会改变返回的数据量),那么在从MongoDB返回结果之后,您可以简单地将返回的文档“还原”为客户机代码中的“键/值”形式。一个简单的JavaScript“shell”示例:

同样的结果是:

{
        "Xylophanes turbata" : {
                "count" : 2,
                "Herbivore subfamily" : "Macroglossinae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24ea",
                                "voucher" : "78-SRNP-10",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        },
                        {
                                "_id" : "5ba6b67ab22f62939eba24eb",
                                "voucher" : "78-SRNP-10.02",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        }
                ]
        },
        "Agrius cingulata" : {
                "count" : 1,
                "Herbivore subfamily" : "Sphinginae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24cc",
                                "voucher" : "77-SRNP-4",
                                "Collection Date" : "1977-06-06T06:00:00.000Z",
                                "Latitude" : "10.83764",
                                "Longitude" : "-85.61871"
                        }
                ]
        }
}

可能需要掌握的主要内容是“聚合”(这是第一个呈现阶段正在做的事情),这可能是您实际希望数据库服务器正在做的事情。根据可用的MongoDB版本,您可以使用或不使用“fancy”操作符。然而,在这里的示例中作为第二阶段演示的“最终结果转换”可能是您真正想要在接受客户机和处理代码时执行的操作。这样做通常更直接、更简洁。

的基本概念是“相反的”。因此,基本上这就是您要做的事情,在适当的地方,以及,因为不需要指定文档中的每个字段,特别是在文档中的字段实际上比问题中的字段多得多的地方,有一些额外的用法

以下内容相当通用,不关心文档中还有多少其他字段:

db.collection.aggregate([
  { "$group": {
    "_id": "$Herbivore species",
    "count": { "$sum": 1 },
    "Herbivore subfamily": { "$first": "$Herbivore subfamily" },
    "records": {
      "$push": {
        "$arrayToObject": {
          "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
            "cond": { "$not": { "$in": [ "$$this.k", ["Herbivore subfamily", "Herbivore species"] ] } }
          }
        }
      }
    }
  }}
])
这将产生如下结果:

{
        "_id" : "Agrius cingulata",
        "count" : 1,
        "Herbivore subfamily" : "Sphinginae",
        "records" : [
                {
                        "_id" : "5ba6b67ab22f62939eba24cc",
                        "voucher" : "77-SRNP-4",
                        "Collection Date" : "1977-06-06T06:00:00.000Z",
                        "Latitude" : "10.83764",
                        "Longitude" : "-85.61871"
                }
        ]
}
{
        "_id" : "Xylophanes turbata",
        "count" : 2,
        "Herbivore subfamily" : "Macroglossinae",
        "records" : [
                {
                        "_id" : "5ba6b67ab22f62939eba24ea",
                        "voucher" : "78-SRNP-10",
                        "Collection Date" : "1978-05-20T06:00:00.000Z",
                        "Latitude" : "10.80212",
                        "Longitude" : "-85.65372"
                },
                {
                        "_id" : "5ba6b67ab22f62939eba24eb",
                        "voucher" : "78-SRNP-10.02",
                        "Collection Date" : "1978-05-20T06:00:00.000Z",
                        "Latitude" : "10.80212",
                        "Longitude" : "-85.65372"
                }
        ]
}
不完全是问题中要求的,因为它当然没有以要求的方式准确显示结果的“关键点”。但这可以通过第二阶段进行修改,与之前相同的运营商展示:

db.collection.aggregate([
  { "$group": {
    "_id": "$Herbivore species",
    "count": { "$sum": 1 },
    "Herbivore subfamily": { "$first": "$Herbivore subfamily" },
    "records": {
      "$push": {
        "$arrayToObject": {
          "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
            "cond": { "$not": { "$in": [ "$$this.k", ["Herbivore subfamily", "Herbivore species"] ] } }
          }
        }
      }
    }
  }},
  { "$group": {
    "_id": null,
    "content": {
      "$mergeObjects": {
        "$arrayToObject": [[
          { "k": "$_id",
            "v": {
              "$arrayToObject": {
                "$filter": {
                  "input": { "$objectToArray": "$$ROOT" },
                  "cond": { "$ne": ["$$this.k", "_id"] }
                }
              }
            }
          }
        ]]
      }
    }
  }},
  { "$replaceRoot": { "newRoot": "$content" } }
])
返回:

{
        "Xylophanes turbata" : {
                "count" : 2,
                "Herbivore subfamily" : "Macroglossinae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24ea",
                                "voucher" : "78-SRNP-10",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        },
                        {
                                "_id" : "5ba6b67ab22f62939eba24eb",
                                "voucher" : "78-SRNP-10.02",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        }
                ]
        },
        "Agrius cingulata" : {
                "count" : 1,
                "Herbivore subfamily" : "Sphinginae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24cc",
                                "voucher" : "77-SRNP-4",
                                "Collection Date" : "1977-06-06T06:00:00.000Z",
                                "Latitude" : "10.83764",
                                "Longitude" : "-85.61871"
                        }
                ]
        }
}
或者,如果您愿意(因为它不会改变返回的数据量),那么在从MongoDB返回结果之后,您可以简单地将返回的文档“还原”为客户机代码中的“键/值”形式。一个简单的JavaScript“shell”示例:

同样的结果是:

{
        "Xylophanes turbata" : {
                "count" : 2,
                "Herbivore subfamily" : "Macroglossinae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24ea",
                                "voucher" : "78-SRNP-10",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        },
                        {
                                "_id" : "5ba6b67ab22f62939eba24eb",
                                "voucher" : "78-SRNP-10.02",
                                "Collection Date" : "1978-05-20T06:00:00.000Z",
                                "Latitude" : "10.80212",
                                "Longitude" : "-85.65372"
                        }
                ]
        },
        "Agrius cingulata" : {
                "count" : 1,
                "Herbivore subfamily" : "Sphinginae",
                "records" : [
                        {
                                "_id" : "5ba6b67ab22f62939eba24cc",
                                "voucher" : "77-SRNP-4",
                                "Collection Date" : "1977-06-06T06:00:00.000Z",
                                "Latitude" : "10.83764",
                                "Longitude" : "-85.61871"
                        }
                ]
        }
}

可能需要掌握的主要内容是“聚合”(这是第一个呈现阶段正在做的事情),这可能是您实际希望数据库服务器正在做的事情。根据可用的MongoDB版本,您可以使用或不使用“fancy”操作符。然而,在这里的示例中作为第二阶段演示的“最终结果转换”可能是您真正想要在接受客户机和处理代码时执行的操作。这样做通常更简单明了。

“我目前正在处理两个独立的查询,…”——一点建议。下次你在这里问问题时,不要说“…我有这个可以工作的代码…”-那么请包括你使用的代码。至少,它表明您已经做出了一些努力,并且还表明了您期望的输出。即使您没有在问题中包含预期输出。如果你能证明自己在尝试解决问题时所做的努力,这会是一个更好的问题。“我目前正在处理两个独立的查询,…”——一点建议。下次你在这里问问题时,不要说“…我有这个可以工作的代码…”-那么请包括你使用的代码。至少,它表明您已经做出了一些努力,并且还表明了您期望的输出。即使您没有在问题中包含预期输出。如果你能证明自己在试图解决问题时所做的努力,那么这个问题会变得更好。