2011-09-22 143 views
2

我想查詢我的MongoDB集合(名稱:包裝)和檢索領域有「網址」與「.com」之間查詢嵌套文檔MongoDB中

結束我不知道如何查詢嵌套的所有文件文件,也使用正則表達式進行查詢。

我實際上是在Perl中編寫它。然而,在mongo shell上運行的查詢也可以。

在此先感謝!

樣本數據:

{ "_id" : ObjectId("4e7a34932cd4b16704000000"), "lastArray" : { "desc" : "google", "url" : "google.com", "data" : [ 
    { 
     "name" : "1", 
     "xpath" : [ ], 
     "nodes" : [ ], 
     "type" : "Any Text" 
    }, 
    { 
     "name" : "2", 
     "xpath" : [ ], 
     "nodes" : [ ], 
     "type" : "Any Text" 
    } 



{ "_id" : ObjectId("4e7a34932cd4b16704000001"), "lastArray" : { "desc" : "yahoo", "url" : "yahoo.com", "data" : [ 
    { 
     "name" : "1", 
     "xpath" : [ ], 
     "nodes" : [ ], 
     "type" : "Any Text" 
    }, 
    { 
     "name" : "2", 
     "xpath" : [ ], 
     "nodes" : [ ], 
     "type" : "Any Text" 
    } 

回答

6
db.wrappers.find({"lastArray.url":{$regex:/\.com$/}}); 
+0

除了我自己,另請參閱Mongodb開發者手冊:http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+對象29% – Dag

0

我不是100%肯定,如果你可以使用正則表達式來搜索子文件的屬性,或者如果你甚至會想出於性能的考慮。在這種情況下,添加「tld」屬性並執行完全匹配查詢可能更容易。像這樣{{「lastArray.tld」:「com」}