简体   繁体   English

转换为Mongodb C#驱动程序

[英]Translate to Mongodb C# Driver

Can someone please help me translate the following into code for the mongodb 2.4 C# driver? 有人可以帮我将以下内容转换为mongodb 2.4 C#驱动程序的代码吗? I have been trying to get this to work but I'm having a hard time figuring it out. 我一直在努力使它起作用,但是我很难弄清楚它。 When I run the following mongo query through the C# drive, I get no records. 当我通过C#驱动器运行以下mongo查询时,没有任何记录。 When I run through the mongo command line I get back 2 records. 当我通过mongo命令行运行时,我会得到2条记录。 I just need to understand what I am doing that is causing the difference in behavior when using the C# driver. 我只需要了解我在执行的操作会导致使用C#驱动程序时行为有所不同。

So far I have the following for the C# driver: 到目前为止,我对C#驱动程序具有以下要求:

 public IEnumerable<ComponentRecordDataModel> GetComponentRecords(
        )
        {
            var componentTypeFilter = Builders<ComponentRecordDataModel>.Filter.Eq(x => x.ComponentType, "Investment");
            var authorizedUserFilter =
                Builders<ComponentRecordDataModel>.Filter.AnyEq(x => x.AuthorizedUserIds, "59e8c1d35e13de1494887658");

            var componentFieldFilters = new List<FieldValueDataModel>
            {
                new FieldValueDataModel()
                {
                    FieldId = "59d664d1c153f67518f98888",
                    Value = "gov"
                },
                new FieldValueDataModel()
                {
                    FieldId = "59d664d1c153f67518f98889",
                    Value = "azure"
                }
            };

            var componentFilters = new List<FilterDefinition<FieldValueDataModel>>();
            foreach (var componentFieldFilter in componentFieldFilters)
            {

                var bsonRegex = new BsonRegularExpression(Regex.Escape(componentFieldFilter.Value), "i");
                componentFilters.Add(Builders<FieldValueDataModel>.Filter.And(new[]
                {
                    Builders<FieldValueDataModel>.Filter.Eq(x => x.FieldId, componentFieldFilter.FieldId),
                    Builders<FieldValueDataModel>.Filter.Eq(x => x.Value, bsonRegex)
                }));
            }

            var fieldFilter = Builders<ComponentRecordDataModel>.Filter.ElemMatch(
                x => x.Fields, Builders<FieldValueDataModel>.Filter.Or(componentFilters)
                );

            var filter = componentTypeFilter & authorizedUserFilter & fieldFilter;
            var renderedFilter = filter.ToJson();

            return MongoContext.Find(filter).ToList();
        }

/ The mongodb query I am trying to replication is shown below / / 我尝试复制的mongodb查询如下所示 /

db.getCollection('componentRecords').aggregate(
    [{
            $match: {
                "componentType": "Investment"
            }
        },
        {
            $match: {
                "authorizedUserIds": {
                    $elemMatch: {
                        $in: ["59e8c1d35e13de1494887658"]
                    }
                }
            }
        },
        {
            $match: {
                "fields": {
                    $elemMatch: { $or:
                     [{ $and : [
                                    { fieldId: ObjectId("59d664d1c153f67518f98888") },
                                    { value: { "$regex": "gov", '$options' : 'i' } }
                                    ]},
                                                           { $and : [
                                    { fieldId: ObjectId("59d664d1c153f67518f98889") },
                                    { value: { "$regex": "azure", '$options' : 'i' } }
                                    ]}                 
                                                                            ] 

                    }
                }
            }
        }
    ])

Have you tried using linq ? 您是否尝试过使用linq? it maybe easier to understand and write: 它可能更容易理解和编写:

var arr = new [] { "59e8c1d35e13de1494887658" };
var id1 = ObjectId("59d664d1c153f67518f98888");
var gov = "gov";

var id2 = ObjectId("59d664d1c153f67518f98889");
var azure = "azure";

collection.AsQuerable().Where(w =>
w.componentType == "Investment" &&
w.authorizedUserIds.Any(a => arr.Contains(a.X)) &&
(
 (w.fieldId == id1 && w.value.ToLower() == go".ToLower()) ||
 (w.fieldId == id2 && w.value.ToLower() == azure.ToLower()) ||
)
).ToList();

Actually I didn't try this code in real example, but worked with linq in mongodb. 实际上,我没有在实际示例中尝试此代码,而是在mongodb中使用linq进行了工作。

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

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