2015-10-07 107 views
1

選擇特定索引字段我希望在"comment_count"= 1MongoDB中

我已經試過此查詢上,例如特定的索引只選擇了「thetext」字段:

db.getCollection('mongotesi').find({},{'bug.long_desc.1.thetext':'1'})

我想秀所有的「thetext」字段也是如此。 這是JSON結構:

{ 
    "_id" : ObjectId("5613c8acc8e53ab811000083"), 
    "@attributes" : { 
     "version" : "4.4.10", 
     "urlbase" : "https://bugs.documentfoundation.org/", 
     "maintainer" : "[email protected]" 
    }, 
    "bug" : { 
     "bug_id" : "31585", 
     "creation_ts" : "2010-11-12 08:55:00 +0000", 
     "long_desc" : [ 
      { 
       "@attributes" : { 
        "isprivate" : "0" 
       }, 
       "commentid" : "194230", 
       "comment_count" : "0", 
       "attachid" : "40242", 
       "who" : "eric.moret", 
       "bug_when" : "2010-11-12 08:55:52 +0000", 
       "thetext" : "Created attachment 40242ooo base fileI am running ooo 3.2.1 OOO320m18 (Build:9502). I am using an odb bas file to perform a mail merge on an odt file under writer. In the mail merge Wizard, on hitting Next in step 6. (Edit Document), the Status window - creating document opens up and generates my mailing. The crash happens reliably while performing this task. I have 118 items to generate.My error report id is: rpmrd6nAttached are the 2 files used for this merge." 
      }, 
      { 
       "@attributes" : { 
        "isprivate" : "0" 
       }, 
       "commentid" : "194232", 
       "comment_count" : "1", 
       "attachid" : "40243", 
       "who" : "eric.moret", 
       "bug_when" : "2010-11-12 08:56:29 +0000", 
       "thetext" : "Created attachment 40243ooo write file" 
      },  
     ], 
    } 
} 
+0

我問昨天的東西非常相似,答案是否定的,蒙戈不支持這個呢。如果你喜歡,我可以爲你提供參考和工具。 –

+0

這裏是我的問題:http://stackoverflow.com/questions/32976362/does-mongodb-have-a-path-wildcard?noredirect=1#comment53778683_32976362我使用節點模塊「樹數學」切片和切塊數據出來時。免責聲明:我寫了樹數學。推廣:樹數學很棒。 –

+0

不是返回確切的鍵值,而是使用[slice]返回特定的位置數組。 – Yogesh

回答

1

陣列採取的[跳過,極限]的形式,其中第一個值表示要跳過的數組中的項目數,第二個值表示要返回的項目數。

使用$slice查詢將查找第一個數組對象

db.collection.find({},{"bug.long_desc":{"$slice":[0,1]},"@attributes":0}) 

,如果你發現了第二個對象,然後跳過第一個對象是這樣

db.collection.find({},{"bug.long_desc":{"$slice":[1,2]},"@attributes":0}) 

編輯

如果打印精確匹配的文本,然後使用forEach爲:

db.collection.find({},{"bug.long_desc":{"$slice":[0,1]},"@attributes":0}).forEach(function(doc){print(doc.bug.long_desc[0].thetext);}) 
+0

好的,非常感謝。現在,如果我只想「thetext」打印,我該怎麼辦? 'db.mongotesi.findOne({},{「bug.long_desc」:{$ slice:[0,1]}})。bug.long_desc' next「.bug.long_desc」我需要添加什麼? - –

+0

@ J.arc檢查已編輯的答案以打印完全匹配的密鑰,然後使用forEach進行查詢 – Yogesh

0

蒙戈沒有做到這一點,但是這將這樣的伎倆:

var tm = require('tree-math'); 
var long_desc=db.getCollection('mongotesi').find({},{'bug.long_desc':'1'}); 
var bugtext = {}; 
tm.find(long_desc,function(path,val {if(path[3]==='thetext')tm.setPath(bugtext,path,val);}); 
console.log(JSON.stringify(bugtext,null,2)); 
+0

謝謝,但林使用R語言,建議? –