user95488
user95488

Reputation: 139

Translate to Mongodb C# Driver

Can someone please help me translate the following into code for the mongodb 2.4 C# driver? 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. When I run through the mongo command line I get back 2 records. I just need to understand what I am doing that is causing the difference in behavior when using the C# driver.

So far I have the following for the C# driver:

 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/

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' } }
                                    ]}                 
                                                                            ] 

                    }
                }
            }
        }
    ])

Upvotes: 0

Views: 939

Answers (1)

Dabbas
Dabbas

Reputation: 3238

Have you tried using 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.

Upvotes: 1

Related Questions