2017-02-23 58 views
1

我有類似於數據庫的數據庫;使用python查找具有特定索引的項目的MongoDB中的文檔

ObjectID(234567654rtfgjytrfgfgf){ 
     _id : "3456ytgfewrtyhgfd" 
     Categories:{ 
      [0] : "Apple" 
      [1] : "Mango" 
     } 
} 
ObjectID(23dfghjthrgytrfgfgf){ 
     _id : "67tghjgfdrthg" 
     Categories:{ 
      [0] : "Orange" 
      [1] : "Mango" 
     } 
} 
ObjectID(23dfghjthrgytrfgfgf){ 
     _id : "67tghjgfdrthg" 
     Categories:{ 
      [0] : "Apple" 
      [1] : "Mango" 
     } 
} 

我想找到具有Apple在使用python在Mongo數據庫文檔0th指標分類排列的文檔。我正在嘗試這種方式;

cursor = collection.find({"Categories" : "Apple"}) 
     for document in cursor: 
      print(document) 

它打印相同的結果,但我怎麼可以指定分類數組的索引?我不指定在collection.find({"Categories" : "Apple"})

回答

1

分類指數僅匹配指數0:

cursor = collection.find({"Categories.0" : "Apple"}) 
for document in cursor: 
    print(document) 
相關問題