2016-01-07 17 views
0
 "structure":{ 
      "country":{ 
       "name":"Some Country" 
      }, 
      "city":{ 
       "name":"Some City" 
      }, 
      "building":{ 
       "name":"Some Building" 
      }, 
      "floor":{ 
       "name":"" 
      } 
     } 

我有一個像上面的對象的集合。我需要查詢收集,因此它只有一個項目的結果。MongoDB查詢至少一個屬性,但更喜歡更理想所有

查詢應該包括所有四個字段的過濾器:國家,城市,建築物和樓層。

如果所有四個匹配都是理想情況。

如果集合中沒有這樣的對象,查詢應該返回3個匹配項和一個空的項。

如果沒有三個以上的比賽,其他兩個應該是空的。

和匹配只有一個字段相同。

重要的是不匹配的其他字段爲空。沒有任何值不匹配,而只是「空白」。

如何爲此寫入mongo查詢?

查詢會的工作對我來說是這樣的:

db.structures.find({"some query that I do no know here"}: { 
       "Some Country", 
       "Some City", 
       "Some Building", 
       "Some Floor" 
       }); 

該查詢會從上面返回JSON對象,但它在收集了對象:

"structure":{ 
     "country":{ 
      "name":"Some Country" 
     }, 
     "city":{ 
      "name":"Some City" 
     }, 
     "building":{ 
      "name":"Some Building" 
     }, 
     "floor":{ 
      "name":"Some Floor" 
     } 
    } 

這將是因爲它被退回全部四場比賽。

+0

它真的會走很長的路,如果你可以構建一些,你填充測試收集一些樣本文檔,目前每個測試用例從與樣本文件的查詢期望輸出的測試用例。 – chridam

+0

@chridam請檢查編輯。 – eomeroff

+0

感謝您的編輯,但仍然不確定是否正確解決了您的問題:因此,如果樓層名稱是「某些樓層」作爲查詢值之一,而其他三個值在數據庫中具有匹配,則您希望查詢將樓層名稱值爲空的文檔返回爲空? – chridam

回答

0

這裏是我想出瞭解決方案。

db.structures.findOne({$or: 
        [ 
        {$and:[ 
        {"country.name": "Some Country"}, 
        {"city.name": "Some City"}, 
        {"building.name": "Some Building"}, 
        {"floor.name": "Some Floor"}] 
        }, 
        {$and:[ 
        {"country.name": "Some Country"}, 
        {"city.name": "Some City"}, 
        {"building.name": "Some Building"}, 
        {"floor.name": ""}] 
        }, 
        {$and:[ 
        {"country.name": "Some Country"}, 
        {"city.name": "Some City"}, 
        {"building.name": ""}, 
        {"floor.name": ""}] 
        }, 
        {$and:[ 
        {"country.name": "Some Country"}, 
        {"city.name": ""}, 
        {"building.name": ""}, 
        {"floor.name": ""}] 
        }] 
       }); 
0

我不知道我完全理解你們需要... 要搜索數據符合4個值,請求應該是這樣的:

db.structures.find({ 
     "country.name":"Some Country", 
     "city.name":"Some City", 
     "building.name":"Some Building", 
     "floor.name":"Some Floor" 
}); 
+0

你還沒有理解這個問題。 – dikesh

+0

如果你可以編輯查詢,所以它加載數據如果匹配3值和第四是「」,如果匹配2值和第三和第四是「」 – eomeroff

+0

即使在SQL中,我不能夠如何寫出這樣的請求? $或運營商是不夠的。 – Kloe2378231

相關問題