1
votes

If I have a data with a structure like this as a single document in a collection:

{
_id: ObjectId("firstid"),
"name": "sublimetest",
"child": {
        _id: ObjectId("childid"),
        "name": "materialtheme"
    }
}

is there a way to search for the embedded document by the id "childid" ? because mongo doesn't index the _id fields of embedded documents (correct me if I am wrong here), as this query doesn't work : db.collection.find({_id:"childid"});

Also please suggest me if there is any other document database that would be suitable for this kind of retreiving data that is structured as a tree, where the requirement is to :

  1. query children without having to issue joins
  2. find any node in the tree as fast as you would find the root node, as if all these nodes were stored as separate documents in a collection.

Why this is not a duplicate of question(s) suggested : the potential-duplicate-question, queries document by using dot notation. But what if the document is nested 7 levels deep ? In such case it would not be suitable to write a query using dot notation. what I want is that, all documents, whether top level, or nested, if they have the _id field, should be in the bucket of _id indexes, so that when you search db.collection.find({_id: "asdf"}), it should take into account documents that are nested too that have the _id field matching "asdf". In short, it should be as if the inner document weren't nested, but present parallel to the outer one.

1
Does this answer your question? stackoverflow.com/questions/30044792/…adamgy
Does this answer your question? MongoDB: How to find a document by an id inside a nested document --> Making it as dupwhoami - fakeFaceTrueSoul
@adamgy, i have added explanation to the answer stating why this is not a duplicate.Siddh Aarth
@whoami, i have added explanation to the answer stating why this is not a duplicate.Siddh Aarth
well you can put indexes on embedded arraysIfaruki

1 Answers

0
votes

You can use the dot notation:

db.posts.find({"child._id": "childid"})