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.

  • Does this answer your question? https://stackoverflow.com/questions/30044792/mongodb-how-to-find-a-document-by-an-id-inside-a-nested-document – adamgy May 31 '20 at 03:54
  • Does this answer your question? [MongoDB: How to find a document by an id inside a nested document](https://stackoverflow.com/questions/30044792/mongodb-how-to-find-a-document-by-an-id-inside-a-nested-document) --> Making it as dup – whoami - fakeFaceTrueSoul May 31 '20 at 05:12
  • @adamgy, i have added explanation to the answer stating why this is not a duplicate. – Siddh Aarth Jun 02 '20 at 06:35
  • @whoami, i have added explanation to the answer stating why this is not a duplicate. – Siddh Aarth Jun 02 '20 at 06:35
  • well you can put indexes on embedded arrays – bill.gates Jun 02 '20 at 06:37
  • @Ifaruki, well , yeah that is the closest thing to acheive that, however if some other document db provides this feature, it would be useful, do you know anything of that sort ? – Siddh Aarth Jun 05 '20 at 10:32

1 Answers1

0

You can use the dot notation:

db.posts.find({"child._id": "childid"})
adamgy
  • 4,543
  • 3
  • 16
  • 31