使用Mongo驱动程序的Azure CosmosDB:获取计数而无需基于C#.Net子文档中的元素获取所有文档 [英] Azure CosmosDB using Mongo Drivers: Get Count With out getting all documents based on element in sub sub document in C#.Net

查看:50
本文介绍了使用Mongo驱动程序的Azure CosmosDB:获取计数而无需基于C#.Net子文档中的元素获取所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is my JSON structure which I'm storing in Azure MongoDB(Pasted the sample structure), I want to query using userID element which is present in Plyrs (Sub Sub document) to get list of all tournaments user has played.

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

  1. I have tried with Aggregate() but not succeeded. Please correct me if I'm missing some thing, below is the code for it.

尝试使用构建器,但未成功,使用构建器,我可以检索到一个级别(sub collection,其中json(粘贴到下面)引用最多为 tbls )我可以在夹层(子子集合)上查询的任何方式.

Tried to get using builders but not succeeded, using builders I'm able to retrieve up to one level (sub collection , with json(pasted below) reference upto tbls ) is there any way that I can query on Plyrs(sub sub collection) .

Azure MongoDB是否支持Aggregate()或不获取整个文档的计数?

Will Azure MongoDB support Aggregate() or the count with out getting the whole documents?

请指导我汇总

   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) } }); 

建筑商

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

错误: 命令聚合失败:不支持'$ group'.

Error : Command aggregate failed: '$group' is not supported.

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

Out Put : For example , Count: 5

推荐答案

并不是所有的MongoDB查询功能都在Cosmos MongoDB API中浮出水面,并且当前,聚合管道是那些尚未实现的功能之一.这就是为什么您收到错误消息,说明不支持$group的原因.

Not all MongoDB query features are surfaced in the Cosmos MongoDB API, and currently the Aggregation Pipeline is one of those features not implemented. This is why you're getting the error stating $group isn't supported.

编辑-自2017年11月起,聚合管道现已成为Cosmos DB的MongoDB API的受支持操作.受支持的功能的完整列表位于此处.

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.

将常规聚合(sum, min, max, avg, count)添加到了DocumentDB API中.

General aggregation (sum, min, max, avg, count) were added a while back to the DocumentDB API.

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

You'd have to do your own aggregation in MongoDB API, for now.

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

Note: count() is supported, so assuming you filter your data (without aggregation pipeline), you can do something like:

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

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

Likewise, you can do a $min or $max via sorting. So for $max, you can do something like this to find the maximum value of foo:

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

这篇关于使用Mongo驱动程序的Azure CosmosDB:获取计数而无需基于C#.Net子文档中的元素获取所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆