【问题标题】:Getting the documents in the specified order from MongoDB using C# driver [duplicate]使用 C# 驱动程序以指定顺序从 MongoDB 获取文档 [重复]
【发布时间】:2014-08-18 03:52:54
【问题描述】:

我是 MongoDB 新手,我想知道是否可以按照我在数组中指定的顺序按 Id 获取文档。我正在使用官方的 c# 驱动程序。

问题描述

我的集合中有一些像这样的文档

{
    "_id" : "1",
    "Name" : "Assignment 1"
}

{
    "_id" : "100",
    "Name" : "Assignment 100"
}

我有一个 ID 数组 { "4", "19", "10", "6" },我需要以与数组中相同的顺序获取指定的文档。

这些是我尝试过的查询

{ "_id" : { "$in" : ["4", "19", "10", "6"] } }

{ "$or" : [{ "_id" : "4" }, { "_id" : "19" }, { "_id" : "10" }, { "_id" : "6" }] }

但是这两个查询都按以下顺序返回文档

但我的预期结果是

现在我面前唯一的选择是为数组中的每个元素发出单独的数据库请求,我认为这是一个不好的选择。

请任何人指导我按照我在数组中指定的顺序获取文档。我正在使用 MongoDB C# 驱动程序并期待基于 C# 的答案。

谢谢,
卡皮尔

【问题讨论】:

标签: mongodb mongodb-.net-driver


【解决方案1】:

好吧,我终于设法使用聚合框架来完成这项工作。

这就是我实现它的方式。但如果有人能提出比这更好的方法,我会很高兴。

string[] fetchingIds = { "4", "19", "10", "6" };

IMongoQuery fetchQuery = Query<Assignment>.In(x => x.Id, fetchingIds);
BsonDocument match = new BsonDocument { { "$match", fetchQuery.ToBsonDocument() } };

BsonDocument currentDocument = null;
BsonDocument finalDocument = null;
BsonValue processingDocument = null;
for (int i = 0; i < fetchingIds.Length; i++)
{
    BsonElement ifStatement = new BsonElement("$eq", new BsonArray(new[] { "$_id", fetchingIds[i] }));
    BsonArray conditionStatement = new BsonArray(new[] { new BsonDocument(ifStatement), BsonValue.Create(i + 1), -1 });
    currentDocument = new BsonDocument("$cond", conditionStatement);

    if (finalDocument == null)
    {
        finalDocument = currentDocument;
    }
    else
    {
        processingDocument = null;
        BsonValue tempDoc = finalDocument["$cond"][2] as BsonDocument;

        if (tempDoc == null)
        {
            processingDocument = finalDocument["$cond"];
        }

        while (tempDoc != null)
        {
            if ((tempDoc["$cond"][2] as BsonDocument) == null)
            {
                processingDocument = tempDoc["$cond"];
                tempDoc = null;
            }
            else
            {
                tempDoc = tempDoc["$cond"][2];
            }
        }

        processingDocument[2] = currentDocument;
    }
}


BsonDocument project = new BsonDocument { { "$project", new BsonDocument 
                                                        { 
                                                           // this will return whose document
                                                            {"obj","$$ROOT"},
                                                            {"weight",finalDocument},
                                                        } 
                                         } };

BsonDocument sort = new BsonDocument { { "$sort", new BsonDocument { { "weight", 1 } } } };

var result = assignmentCollection.Aggregate(new AggregateArgs
{
    Pipeline = new[] { match, project, sort },
}).ToList();

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 2020-08-14
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多