Knows Not Much
Knows Not Much

Reputation: 31546

Query MongoDB with DBRef

I have a table in mongo db called documents

{
    "_id" : ObjectId("565a7f1"), 
    "_class" : "Document", "name" :
    "Book Template", 
    "location" : "/Book Template.pdf", 
    "type" : DBRef("types", ObjectId("3eaabf5")) 
},
{
    "_id" : ObjectId("565a7f2),
    "_class" : "Document", 
    "name" : "Sample Contract", 
    "location" : "/Sample Contract.pdf", 
    "type" : DBRef("types", ObjectId("3eaabf5")) 
},
{ 
    "_id" : ObjectId("565a7f3"), 
    "_class" : "Document", 
    "name" : "Clustering with RabbitMQ", 
    "location" : "/Clustering with RabbitMQ.txt", 
    "type" : DBRef("types", ObjectId("3eaabf6")) 
}

and then I have a table called types

{ 
    "_id" : ObjectId("3eaabf5"), 
    "_class" : "Type", 
    "name" : "PDF", 
    "description" : "Portable Document Format", 
    "extension" : ".pdf" 
};
{ 
    "_id" : ObjectId("3eaabf6"), 
    "_class" : "Type", 
    "name" : "NOTE", 
    "description" : "Text Notes", 
    "extension" : ".txt" 
},
{ 
     "_id" : ObjectId("3eaabf7"), 
     "_class" : "Type",
     "name" : "WEB", 
     "description" : "Web Link", 
     "extension" : ".url" 
}

I want to query the documents table for all documents which have an extension of PDF.

So I wrote the following queries... but everything returns empty results.

>db.documents.find({"types.$id":"3eaabf5"})
>
>db.documents.find({"types.name":"PDF"})
>

Upvotes: 2

Views: 3720

Answers (1)

max
max

Reputation: 2817

You may try this

db.documents.find({"type.$id":ObjectId("3eaabf5")}

Upvotes: 1

Related Questions