mongodb - 使用 Mongo 驱动程序的 Azure CosmosDB : Get Count With out getting all documents based on element in sub sub document in C#.Net

标签 mongodb azure azure-cosmosdb

下面是我存储在 Azure MongoDB 中的 JSON 结构(粘贴示例结构),我想使用 Plyrs 中存在的 userID 元素进行查询( Sub Sub 文档)来获取用户参加过的所有锦标赛的列表。

  1. 我尝试过 Aggregate() 但没有成功。请纠正我 如果我遗漏了一些东西,下面是它的代码。

  2. 尝试使用构建器但没有成功,使用构建器我可以检索最多一个级别(子集合,带有 json(粘贴在下面)引用最多 tbls )我可以通过任何方式查询 Plyrs(子子集合)。

  3. Azure MongoDB 是否支持 Aggregate() 或在不获取整个文档的情况下进行计数?

请指导我聚合

   var count = sampleMongoDataBase.GetCollection<GameInfo>(sampleCollectionName).Aggregate()
                    .Unwind<GameInfo, TableDocumet>(t => t.Tables)
                    .Unwind<TableDocumet, PlayerDocument>(p => p.Players)
                   .Match(x => x.UserID == "3fd30fa8-f5c5-4311-8741-4032142bbb33")
                   .Group(new MongoDB.Bson.BsonDocument { { "id", "$Plyrs._id" }, { "count", new MongoDB.Bson.BsonDocument("$sum", 1) } }); 

build 者

 mongoDataBase.GetCollection<GameInfo>(mongoCollectionName).Find(Builders<GameInfo>.Filter.ElemMatch(x => x.Tables,t => t.TableId == 1)).Count();

错误: 命令聚合失败:不支持“$group”。

JSON:

`

{
"TnId": 651,
"QnId": "7a5de5dc-b02f-4013-9d02-ebf8430e7a56",
"samt": 10,
"tbls": [
  {
    "TblId": 1,
    "Plyrs": [
      {
        "_id": 1048995,
        "UserId": "FC381A6D717B6973-711BD743AE90E6D7",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Roman Yates",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1048996,
        "UserId": "fc3e0971-2c3c-e697-b68a-33b12a207bc2",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Neil Gaines",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1048997,
        "UserId": "FC369812-6DC04066-A133-1C10EEBA1546",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Lori Reyes",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1048998,
        "UserId": "FC2AC461-C7FAD800D3D79A2CD2680C72",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Duane Hoffman",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1048999,
        "UserId": "FC39AFCF-688C-B61A-A0DB-80EADAC832CD",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Alfredo Ware",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1049000,
        "UserId": "FC2B863D-9ACC-13C706EA-8E31917566DB",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Abigail Riddle",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      }
    ]
  },
  {
    "TblId": 2,
    "Plyrs": [
      {
        "_id": 1049001,
        "UserId": "FC2BCC93-DDAE-4622-8D1226BDF1181D63",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Rene Spence",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1049002,
        "UserId": "FC3D0CF42017-C3BFF460-E8DBDE3D1D77",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "TU006918",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1049003,
        "UserId": "FC2CDE495F34E557-77C8-4B10981B7758",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Marshall Lutz",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1049004,
        "UserId": "FC3EE1FDB4913FF345AE90CD14BE3607",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Roberto Burton",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1049005,
        "UserId": "FC378E3C-C2A0528F094A-7FB31BD9D278",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Summer Stephenson",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      },
      {
        "_id": 1049006,
        "UserId": "FC431BBB-A79D45A0EAB034676C755E5A",
        "St": 3,
        "PCS": 2,
        "ConnId": null,
        "DN": "Terence Fischer",
        "RB": 2,
        "LL": 1,
        "Chips": 5000,
        "Rank": 46,
        "ExitLvl": 0,
        "TRA": 0,
        "PRI": "",
        "IsAns": false
      }
    ]
  }

输出: 例如,计数:5

最佳答案

并非所有 MongoDB 查询功能都出现在 Cosmos MongoDB API 中,目前聚合管道是尚未实现的功能之一。这就是为什么您收到错误消息,指出不支持 $group

EDIT - As of November 2017, Aggregation Pipeline is now a supported operation with the MongoDB API of Cosmos DB. Full list of supported features is here.

不久前,DocumentDB API 中添加了常规聚合(sum、min、max、avg、count)。

目前,您必须在 MongoDB API 中进行自己的聚合。

注意:支持 count(),因此假设您过滤数据(没有聚合管道),您可以执行以下操作:

db.collection.find({...}).count()

同样,您可以通过排序执行 $min$max 。因此,对于 $max,您可以执行以下操作来查找 foo 的最大值:

db.collection.find({...},{foo:1}).sort({foo:-1}).limit(1)

关于mongodb - 使用 Mongo 驱动程序的 Azure CosmosDB : Get Count With out getting all documents based on element in sub sub document in C#.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844678/

相关文章:

sql - 带/不带 "ORDER BY"的 CosmosDB sql 查询返回不同数量的项目

typescript - 为自定义数据库解决方案创建 Microsoft Bot Framework 状态存储适配器的指南

MongoDB(Java): How to cancel map-reduce (or aggregate)

node.js - "How to get ' 父级到子级 ' relation with mongodb aggregation"

mongodb - 服务器端的 Meteor.subscribe

azure - 在 Cosmos DB 中获取不同的值

asp.net - 我可以使用Environment.GetEnvironmentVariable方法从Azure应用程序设置中读取环境变量吗?

node.js - 按 Id 显示 React 属性

azure - 如何使用 Gremlin API 在 Azure Cosmos DB 中创建元图

azure - DocumentDb 上的文档未被替换