0

I am using MongoDB C# driver version 2.2. My collection contains "Parent" objects. Each parent object has an array of children objects. Each child has name value:

"parent": {
    "children":[
        { "name": "Bob", "age": 10},
        { "name": "Alice", "age": 7},
        { "name": "Tobias", "age": 11}
    ]
}

I need to translate the following code into C# statements / LINQ syntax:

db.getCollection('Parents').find({'parent.children': { $elemMatch: { 'name': { $regex: '.*ob.*', $options: 'im' } }}})

I have found there are methods like

var builder = Builders<BsonDocument>.Filter;
builder.Regex("parent.children.name", new BsonRegularExpression(".*ob.*")); //does not work with array

and

builder.AnyEq("parent.children.name", "ob"); //without regex

But I cannot understand how to combine them. Please advise.

UPDATE:

I am using the following for now, please correct me if you know a reason why it should not work correctly:

builder.AnyEq("parent.children.name", new BsonRegularExpression(".*ob.*"))
1
  • Your final query looks fine to me, btw! Commented Aug 1, 2016 at 13:17

3 Answers 3

2

I am using the following for now:

builder.AnyEq("parent.children.name", new BsonRegularExpression(".*ob.*"))
Sign up to request clarification or add additional context in comments.

Comments

1

Can't test C# on this machine. Let me know if this doesn't work:

var filter = Builders<People>.Filter.ElemMatch(x => x.Parent.Children, x => Regex.IsMatch(x.Name, "regex"));
var res = await collection.Find(filter).ToListAsync();

Here's a trick you might like btw:

// Take your inputted `find` query string:
string bsonQuery = "{'parent.children': { $elemMatch: { 'name': { $regex: '.*ob.*', $options: 'im' } }}}";

// Use it as the filter!
var filter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(bsonQuery);

// Results:
var result = col.FindSync (filter).ToList();

5 Comments

In your example you use classes (strongly-typed). All I have is a dotted notation of the "path". Can you please improve your example?
@Antipod Look up COPOs. They'll make your life a lot easier when querying using C#
Thank you for the update. I am almost sure the provided update will work. The issue is: it is a string and I do not want to construct strings, we have the methods in the driver which we can use and the driver will construct queries for us, after all: ) Speaking about POCOs, I 100% agree with you, but I have a requirement - JSON does not have a defined structure, it is dynamic.
Whilst JSON is dynamic, your first-class objects in C# are not. So writing COPOs on a use-by-use basis even if the data may change makes sense for the C# code operating on them.
thank you for your opinion. Unfortunately, POCO is an off-topic in my question, sorry for that :) BTW, I have updated the question.
0

I've tested your current expression (builder.AnyEq("parent.children.name", new BsonRegularExpression(".*ob.*")) on a database at home and I don't believe it behaves in the way you intend.

Although the c# documentation doesn't explicitly state this, mongoDB supports the Regex filter on an array field. I have tested the below expression in C# and have correct results for the Regex despite the field being an array.

builder.Regex(MONGO_FIELD_NAME, new BsonRegularExpression("SOME REGEX"));

In addition, I've tested Regex on the toy example in the [online mongo webshell for query arrays] (https://docs.mongodb.com/manual/tutorial/query-arrays/). Querying for db.inventory.find({tags : {$regex : "^bl"}} will return results with "blank" or "blue" despite the "tag" field being an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.