2015-03-25 39 views
1

我正在使用一些嵌套的文檔......如何才能檢查 存在只是一個關鍵?MongoDB:存在一個嵌套鍵

即,如果我想返回任何有嵌套鍵「工作」,其中父鍵是動態的任何記錄。

這裏是應該返回一條記錄:

{ 
    "_id" : ObjectId("123"), 
    "customer_name" : "test user", 
    "123456" : { 
      "home" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 
      "work" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 

    }, 
    "321456" : { 
      "home" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 
      "work" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 

    }, 
    "789654" : { 
      "home" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 
      "work" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 

    }, 
    "createad" : "2011-04-14 16:44:09" 
} 

這裏是一個不應該被返回的記錄:

{ 
    "_id" : ObjectId("123"), 
    "customer_name" : "test user", 
    "123456" : { 
      "home" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 
    }, 
    "321456" : { 
      "home" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 
      "home1" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 

    }, 
    "321543" : { 
      "home" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 
      "home1" : { 
        "id" : "1536734296", 
        "last" : "2011-04-14 16:44:09", 
      } 

    }, 
    "createad" : "2011-04-14 16:44:09" 
} 

我想這會是這樣的: db.find( {「* .work」:{「$ exists」:true}}) 任何想法?

+1

'*。工作「將無法正常工作。你需要父母的鑰匙。 – styvane 2015-03-25 10:11:18

+0

我知道,這就是爲什麼我發佈了這個問題的原因...... – marnun 2015-03-25 10:28:21

+0

如果你不能像chridam正確的建議那樣修復這個模式,那麼你需要使用['$ where'](http://docs.mongodb。 org/manual/reference/operator/query/where /#op._S_where)並且表現糟糕。 – JohnnyHK 2015-03-25 13:32:37

回答

1

將值作爲鍵並不是很好的做法,並且當前(也可能在未來),無法使用字段名中的通配符來查詢MongoDB集合。所以我的建議是遵循這一模式架構的重新設計:

"_id" : ObjectId("55128d6315f3df650f2038cf"), 
"customer_name" : "test user", 
"createad" : "2011-04-14 16:44:09", 
"data" : [ 
    { 
     "key" : "123456", 
     "home" : { 
      "id" : "1536734296", 
      "last" : "2011-04-14 16:44:09" 
     }, 
     "work" : { 
      "id" : "1536734296", 
      "last" : "2011-04-14 16:44:09" 
     } 
    }, 
    { 
     "key" : "321456", 
     "home" : { 
      "id" : "1536734296", 
      "last" : "2011-04-14 16:44:09" 
     }, 
     "work" : { 
      "id" : "1536734296", 
      "last" : "2011-04-14 16:44:09" 
     } 
    }, 
    { 
     "key" : "789654", 
     "home" : { 
      "id" : "1536734296", 
      "last" : "2011-04-14 16:44:09" 
     }, 
     "work" : { 
      "id" : "1536734296", 
      "last" : "2011-04-14 16:44:09" 
     } 
    } 
] 

}

,您可以查詢如下

db.collection.find({ "data.work" : { $exists : true, $ne : null } }) 

data陣列來檢查work場的存在。

+0

我有一個不同的問題,阻止我在數據模式中進行重新設計 – marnun 2015-03-25 11:47:53

0

如果您的父母關鍵不同,您將不得不通過它獲取所有數據到您的應用程序循環。 由於mongodb存儲數據的方式,如果您有不同的父鍵,您無法進行查詢。你將不得不把所有的文件都放到你的應用程序中,然後循環並自行檢查。

如果你有大量的數據,你不希望所有的數據到你的應用程序我建議你以不同的方式安排你的數據,這樣你將有一個方式來查詢數據更快