2011-05-18 68 views
2

我的表有一個由一個字符串索引的數組,我希望所有的記錄匹配這個字符串,不管它是什麼值。例如讓所有的記錄wher ID1是填寫:是否可以在數組鍵上查詢mongodb?

var a = { 
    type: "Information", 
    ids: { 
    'id1' : '123' 
    'id2' : '456' 
    } 
}; 


var b = { 
    type: "Information", 
    ids: { 
    'id1' : '789' 
    } 
}; 

是否有可能做到這一點與MongoDB的又如何?

回答

7

您可以使用$存在這樣的:

> db.things.insert({'type': 'Information', 'ids':{'id1': 123, 'id2': 456}}) 
> db.things.insert({'type': 'Information', 'ids':{'id1': 746, 'id2': 456}}) 
> db.things.insert({'type': 'Information', 'ids':{'id2': 456, 'id3': 936}}) 

> db.things.find({'ids.id1': {'$exists': true}}) 
{ "_id" : ObjectId("4dd3c706938307861ed610dd"), "type" : "Information", "ids" : { "id1" : 123, "id2" : 456 } } 
{ "_id" : ObjectId("4dd3c7a1938307861ed610de"), "type" : "Information", "ids" : { "id1" : 746, "id2" : 456 } } 
+1

你如何創建索引這種類型的查詢? – 2012-12-03 19:30:23