0

Have kind of a MongoDB issue here as Im trying to find a collection om matching IDs. As Im building kind of a tree structure based on MongoDB. I have objects that contains multiple attributes like name, parents and children for them to know there relations. Right now I can from and object go though its child ID and load each object one by one. I how ever noticed its much faster got get them as a collection.

So my question is now, how do I find ID of intrest in the collection on tree node objects represented in MongoDB.

example of how the mongoDB document can look

{
  "_id" : ObjectId("523de6c6d7b47f1f74c98ba2"),
  "IDType" : "root",
  "Name" : "test root",
  "Relations" : [{
      "Children" : {
        "ChildrenList" : ["523de725d7b47f1d0c8242f1", "523de746d7b47f1d0c8242f5"]
      },
      "Parents" : {
        "ParentList" : []
      }
    }]
}

So, how would I find one of the IDs by matching by a ID in the ChildrenList ? this as Relations is a array of Children and Parents. and these two contains array of IDs to be matched.

have tried this with out sucess.

IMongoQuery searchQuery = Query.ElemMatch("Children", Query.EQ("ChildrenList", "523de746d7b47f1d0c8242f5"));
3
  • 1
    The "Children" field is a descendant of "Relations" so minimally you'll need to include that in your query. "Relations.Children" Commented Nov 23, 2013 at 12:36
  • I also don't understand why the children and parents are stored in an array off of Relations, and then have an extra object level instead of being stored directly. Commented Nov 23, 2013 at 12:39
  • That's because there should be a possibility to have multiple parent-node-child relations for a single object. Not that I say that is usual, but as I have been in scenarios where this can be in use, I wanted to make this possible in the easiest way. Otherwise you have to fake it, and that can be quiet messy. Commented Nov 23, 2013 at 13:20

1 Answer 1

1

Ok, as I found the solution thanks to WiredPrairie I thought I should post it. The solution was apperantly not using ElemMatch for the parts but writing it like this.

IMongoQuery searchQuery = Query.EQ("Relations.Parents.ParentList", objectID);

Thanks anyways for the help to the right direction.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.