使用DBRef查询MongoDB

时间:2015-11-29 05:18:10

标签: mongodb mongodb-query

我在mongo db中有一个名为documents

的表
   function loadpage(){ //runs when the browser first loaded
        var mov = JSON.parse(localStorage.getItem('one'));
        if (mov == null || mov == "") {
            alert('Please choose object')
            window.location = "object.html"
        }else{
           getdetail();
           gentable();
           retrivedata();
           document.getElementById("proceed").disabled = true;
        }
   }

然后我有一个名为types

的表
{
    "_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")) 
}

我想在文档表中查询所有扩展名为PDF的文档。

所以我编写了以下查询...但是所有内容都返回空结果。

{ 
    "_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" 
}

1 个答案:

答案 0 :(得分:0)

你可以试试这个

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