0
var query = Query<MongoData>.EQ(e => e.name, someString);
var entity = collection.FindOneAs<MongoData>(query);

And this returns the MongoData object in collection where the property name matches the string (someString) I send it.

I'd like to add another query to this to also get the entity that matches name and files.author

collection is an List, and files is an List inside of collection.

Currently, I'm doing it the hard way and just looping through entity.files until I find a match, but this is painful.

So given a MongoData List that contains one object with a name of X and an files list that contains an author of Y, I'd like to return this one.

6
  • 1
    @poke Why do you need to see the rest of the code to answer my question? Commented Mar 20, 2014 at 21:55
  • Because it puts me off to see two opening curly braces in a broken method definition like that. It looks like a copy/paste error. And if it doesn’t matter to the question, then what’s the point of posting it as a method (especially with a try block) in the first place? Break it down to the important parts. Commented Mar 20, 2014 at 21:58
  • @poke Okay, I edited it out. Commented Mar 20, 2014 at 21:59
  • 2
    Are you really using ArrayLists? Consider using a generic like List<>. This will avoid boxing/unboxing performance hit, if you hold value types, as these will be cast to object on insertion, and cast back upon read. Read more about it here Commented Mar 20, 2014 at 22:01
  • @Andreas No, I'm not really using ArrayLists. I actually forgot what I was using and said "ArrayLists" because that's what I learned to use first. They're just Lists. But thanks for telling me why I should use one over the other. Commented Mar 20, 2014 at 22:04

1 Answer 1

2

You could probably do it like this and query multiple properties at once:

var query = Query.And(
    Query<MongoData>.EQ(e => e.name, someString),
    Query<MongoData>.ElemMatch(
        e => e.files,
        q => q.EQ(f => f.author, someAuthorName)
    )
);

The latter subquery uses the ElemMatch<TValue> query function, which expects an expression returning an enumerable of subobjects, and a function that executes another query on each of those subobjects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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