mongodb - mongo複雜查詢問題
我想大声告诉你
我想大声告诉你 2017-05-02 09:25:22
0
4
706

原json

{ "_id" : ObjectId("58b3a8dc96fbc7cfb8287093"), "name" : "《地下水质量标准》", "GBNumber" : "GB/T 14848-93", "dataEntryClerk" : "handsomeboy", "lastModified" : "Fri Feb 27 2017 12:03:40 GMT+0800", "contents" : [ { "standardID" : "9527-01", "unit" : "mg/L", "classifications" : [ { "level" : "I", "upperLimit" : "0", "lowerLimit" : "50" }, { "level" : "II", "upperLimit" : "50", "lowerLimit" : "150" }, { "level" : "III", "upperLimit" : "150", "lowerLimit" : "250" }, { "level" : "IV", "upperLimit" : "250", "lowerLimit" : "350" }, { "level" : "V", "upperLimit" : "350", "lowerLimit" : "-1" } ] }, { "standardID" : "7439-89-6", "unit" : "mg/L", "classifications" : [ { "level" : "I", "upperLimit" : "0", "lowerLimit" : "0.1" }, { "level" : "II", "upperLimit" : "0.1", "lowerLimit" : "0.2" }, { "level" : "III", "upperLimit" : "0.2", "lowerLimit" : "0.3" }, { "level" : "IV", "upperLimit" : "0.3", "lowerLimit" : "0.4" }, { "level" : "V", "upperLimit" : "0.5", "lowerLimit" : "-1" } ] } ] }

想查詢出來的結果:

{ "contents" : [ { "standardID" : "9527-01", "unit" : "mg/L", "classifications" : [ { "level" : "I", "upperLimit" : "0", "lowerLimit" : "50" }, { "level" : "II", "upperLimit" : "50", "lowerLimit" : "150" }, { "level" : "III", "upperLimit" : "150", "lowerLimit" : "250" }, { "level" : "IV", "upperLimit" : "250", "lowerLimit" : "350" }, { "level" : "V", "upperLimit" : "350", "lowerLimit" : "-1" } ] } ] }

請問下各位大牛db.xxx.find() 怎麼寫呢

我想大声告诉你
我想大声告诉你

全部回覆 (4)
刘奇

先提個意見:

提問題,把最小化可復現問題的例子放出來就好,你這放這麼長一文檔,大家看的很累。


你需要的其實是返回數組中某些特定文檔,而不是整個數組。

1 如果你只需要回傳陣列中的一個元素

db.xxx.find( {'contents.standardID': '9527-01'}, {contents: {$elemMatch: {standardID: '9527-01'}}, _id: 0} )

第一行是查詢條件,第二行是篩選條件。 可以看到操作符也可以用在篩選條件裡面。但是這個運算子只會傳回滿足條件的第一個元素,而需要2.2版本以上的MongoDB。

或使用$下標選擇符:

db.xxx.find( {'contents.standardID': '9527-01'}, {'contents.$': 1, _id: 0} )

上面範例中的查詢條件也可以使用$elemMatch

如果你需要傳回數組中多個符合的元素:

2$unwind

透過把$unwind来把contents來把contents當作一個獨立的文檔流來進行操作,程式碼請見@bguo的回答。
但是如果你的陣列很大,這會導致效能問題。

3$filter

這是一個3.2版本中新出的操作符,用來過濾回傳的內容。

db.xxx.aggregate( {$match: {'contents.standardID': '9527-01'}}, {$project: { contents: {$filter: { input: '$contents', as: 'contents', cond: {$eq: ['$$contents.standardID', '9527-01']} }}, _id: 0 }} )

當然你還可以使用$redact(2.6版本), 或者mapReduce()等等多種方法。

    为情所困

    查詢條件。比如 name

    db.xxx.find({"name" : "《地下水质量标准》" } ,function(err, data){ if(data){ console.log(data.contents) } })
      给我你的怀抱

      一種實作方法:利用MongoDB的Aggregate。

      1、先對包含array的contents施加$unwind

      2、然後利用$match,施加過濾條件

      3、最後利用$project,保留所需的field

      參考下方的程式碼

      db.test1.aggregate([ {$unwind : "$contents"}, {$match : { name : "《地下水质量标准》", "contents.standardID" : "9527-01"}}, { $project : { contents : 1 , _id : 0 }} ])

      供參考。

      Love MongoDB! Have Fun!

      ------------------------華麗的分割符--------------------- -----------

      MongoDB中文社群線下活動繽紛,請猛戳下方:

      2017華山論劍|MongoDB中文社群

      三月杭州站在即! ! ! 有興趣的朋友火速報名! ! !

        给我你的怀抱

        雷雷

          最新下載
          更多>
          網站特效
          網站源碼
          網站素材
          前端模板
          關於我們 免責聲明 Sitemap
          PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!