1

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.

6
  • Does this answer your question? stackoverflow.com/questions/30044792/… Commented May 31, 2020 at 3:54
  • Does this answer your question? MongoDB: How to find a document by an id inside a nested document --> Making it as dup Commented May 31, 2020 at 5:12
  • @adamgy, i have added explanation to the answer stating why this is not a duplicate. Commented Jun 2, 2020 at 6:35
  • @whoami, i have added explanation to the answer stating why this is not a duplicate. Commented Jun 2, 2020 at 6:35
  • well you can put indexes on embedded arrays Commented Jun 2, 2020 at 6:37

1 Answer 1

0

You can use the dot notation:

db.posts.find({"child._id": "childid"}) 
Sign up to request clarification or add additional context in comments.

1 Comment

I think, you would like to use ObjectId class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.