简体   繁体   English

C# MongoDB 查询中的索引值始终为 0 展开

[英]index value always 0 in C# MongoDB query with unwind

I received some great help to get the MongoDB query right: Is there a way to add a counter to mongodb query?我得到了一些很大的帮助来获得 MongoDB 查询权: 有没有办法在 mongodb 查询中添加一个计数器?

When I translate that query into C#, after the 2nd unwind phase, the index is always 0. I'm trying to use that value as a resultId.当我将该查询转换为 C# 时,在第二个展开阶段之后,索引始终为 0。我试图将该值用作 resultId。

Here's a snippet of the data:这是数据的片段:

"_id": ObjectId("5d1b9aea5c1dd54e8c773f42")
"timestamp":    
    [ 
      "systemTimestamp":    2019-07-02T17:56:53.765+00:00
      "serverTimestamp":    0001-01-01T00:00:00.000+00:00
      "systemTimeZone":     "System.CurrentSystemTimeZone"
    ]
"urlData":
    [0]:
    "fullUrl":"https://imgur.com/gallery/EfaQnPY"
"UID":"00000-W3W6C42GWTRE960"
"safety": "safe"

Here's how I translated that into C# begin & end are DateTime parameters passed to the method这是我如何将其翻译成 C# 开始和结束是传递给方法的 DateTime 参数

var match1 = new BsonDocument("$match",                    
    new BsonDocument("timestamp.serverTimestamp", new BsonDocument { { "$gte", begin.ToUniversalTime() }, { "$lte", end.ToUniversalTime() } });


// unwind flattens the urlData array
var unwind1 = new BsonDocument("$unwind", new BsonDocument
                {
                    { "path", "$urlData" },
                    { "includeArrayIndex", "index" }
                });

// match2 grabs only the first element of the urlData array
var match2 = new BsonDocument("$match", new BsonDocument
                {
                    { "index", 0 },
                    { "urlData.fullUrl",  new BsonDocument{ { "$regex", cleanedUrl }, { "$options", "i" } } }
                });
// group pushes all previous results into 1 array (this is to get resultId set)
var groupBy = new BsonDocument("$group", new BsonDocument
                {
                    { "_id", BsonNull.Value },
                    { "data", new BsonDocument("$push", "$$ROOT") }
                });

// now we unwind the array and have each result with resultId which starts at 0
var unwind2 = new BsonDocument("$unwind", new BsonDocument
                {
                    { "path", "$data" },
                    { "includeArrayIndex", "resultId" }
                });

// Add 1 to the resultId to get the right value
var addFields = new BsonDocument("$addFields",
                new BsonDocument("data.resultId",
                new BsonDocument("$sum",
                    new BsonArray
                    {
                            "resultId",
                            1
                    })));

var replaceRoot = new BsonDocument("$replaceRoot", new BsonDocument("newRoot", "$data"));

// separates out date, safetyRating and UID
var project = new BsonDocument("$project",
            new BsonDocument
            {
                    { "_id", 0 },
                    { "accessTime", new BsonDocument("$dateToString", new BsonDocument {
                        { "format", "%Y-%m-%d" }, { "date", "$timestamp.serverTimestamp" }
                    }) },
                    { "safetyRating", "$safety" },
                    { "url", "$urlData.fullUrl" },
                    { "userId", "$UID" },
                    { "resultId", "$resultId" }
            });

// sort by date
var sort = new BsonDocument("$sort", new BsonDocument("date", 1));

var pipeline = new[] { match1, unwind1, match2, groupBy, unwind2, addFields, replaceRoot, project, sort };

List<APIaccessedURL> queryResults = await collection.Aggregate<APIaccessedURL>(pipeline).ToListAsync();

Results in resultId always being 1 (I verified that without the $addFields stage, the resultId is 0).结果 resultId 始终为 1(我验证没有 $addFields 阶段,resultId 为 0)。

{
    "resultId": 1,
    "userId": "00000-W3W6C42GWTRE960",
    "url": "https://imgur.com/gallery/fxQuNMw",
    "safetyRating": "safe",
    "accessTime": "2019-07-02T00:00:00Z"
},
{
    "resultId": 1,
    "userId": "00000-W3W6C42GWTRE960",
    "url": "https://imgur.com/",
    "safetyRating": "safe",
    "accessTime": "2019-07-02T00:00:00Z"
},

Ideas on what I'm doing wrong would be greatly appreciated!关于我做错了什么的想法将不胜感激!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM