Getting documents using DBRef ID

Hello! I am fairly new to MongoDB.

My documents look like this:

{
    "_id": {
        "$oid": "6399b877ef3a1ecda901bb7b"
    },
    "parentProduct": {
        "$ref": "Product",
        "$id": {
            "$oid": "6399b877ef3a1ecda901bb7a"
        }
    },
    "identifier": "someKindOfIdentifier",
    "tags": []
}

“parentProduct” is a DBRef(). How do I get the all the documents with a parent product of “6399b877ef3a1ecda901bb7a”?

This works:

{"parentProduct.$id": ObjectId('6399b877ef3a1ecda901bb7a')}

But for some reason this does not work? It does not match any document.

{"parentProduct.$id.$oid": "6399b877ef3a1ecda901bb7a"}

I am even more confused because this works:

{"parentProduct.$ref": "Product"}

According to the MongoDB documentation, we should be able to use the dot notation to access things (e.g. apple.tree.root)

Why does it not work when using “parentProduct.$id.$oid”?
And is there any way for me to get what I want?

Thank you for your time!

1 Like