2014-12-07 65 views
2

如果我在我的架構中有像這樣的嵌入文檔:

location: { 
    coordinates: [-41.588221, 71.123812], 
    unitNumber: '4a', 
    streetAddress: '1 Abc Lane', 
    <Other location-related data> 
} 

我要排除的文件和其他使用$nin位置的基礎上,但我只有unitNumbercoordinates,有沒有辦法正確做到這一點。

例如,說我想排除從結果集的位置下面的數組:

locations = [ 
    {coordinates: [-41.2342432, 71.812312], unit: '4a'}, 
    {coordinates: [-40.2242432, 70.212352], unit: '7d'}, 
    {coordinates: [-42.2546432, 72.312312], unit: '10b'}, 
    {coordinates: [-41.2342132, 61.812312], unit: '1z'} 
] 

有沒有辦法做到這一點給出了上述模式?我不相信使用{location: {$nin: locations}}會起作用,因爲它要求locations中的每個對象都要使整個嵌入式文檔符合排除條件。

回答

1

爲了排除與某些locationunit對匹配的文檔。

  • 撰寫一個條件對象,它匹配我們需要排除的所有文檔。
  • 這是使用$all運算符爲coordinates字段完成的。
  • 使用邏輯$nor運算符排除匹配的文檔。

避免使用$nin運營商儘可能optimize您的查詢。

查詢組成需要在客戶端做,

var locationsToExclude= [ {coordinates: [-41.2342432, 71.812312], unit: '4a'}, 
          {coordinates: [-40.2242432, 70.212352], unit: '7d'}, 
          {coordinates: [-42.2546432, 72.312312], unit: '10b'}, 
          {coordinates: [-41.2342132, 61.812312], unit: '1z'} 
         ] 

var findCondition = {$nor:[]}; 
locationsToExclude.forEach(function(i){ 
    var coordinates = i.coordinates; 
    var unitNumber = i.unit; 
    var condition = {"coordinates":{$all:coordinates}, 
        "unitNumber":unitNumber}; 
    findCondition.$nor.push(condition); 
}) 
db.location.find(findCondition); 
+0

這工作完美,謝謝!也擔心'$ nin'的表現,所以我很高興這是解決方案:) – jtmarmon 2014-12-08 21:57:14

0

您可以使用aggregate項目只有座標和單元的位置,做一個匹配條件與$nin和項目再次得到原始文檔使用的數據來自$$ ROOT:

db.location.aggregate([ 
    {$group : { _id : "$location", data: { $push: "$$ROOT" }}}, 
    {$project: { _id: { coordinates: "$_id.coordinates", unit: "$_id.unitNumber" }, data : 1}}, 
    {$match : {_id : {$nin : [ 
      {coordinates: [-41.2342432, 71.812312], unit: '4a'}, 
      {coordinates: [-40.2242432, 70.212352], unit: '7d'}, 
      {coordinates: [-42.2546432, 72.312312], unit: '10b'}, 
      {coordinates: [-41.2342132, 61.812312], unit: '1z'} ] 
    }}}, 
    {$unwind: "$data"}, 
    {$project: {_id: 0, location: "$data.location"}} 
]); 

在過去的$項目中,添加你需要從原始文檔(數據),以獲得更多的領域,例如,field1: "$data.field1", field2: "$data.field2"