2017-08-21 26 views
-5
{ 
    TypeList" : [ 
     { 
      "TypeName" : "Carrier" 
     }, 
     { 
      "TypeName" : "Not a Channel Member" 
     }, 
     { 
      "TypeName" : "Service Provider" 
     } 
    ] 
} 

問:與蒙戈文本搜索和操作多個單詞部分enered

db.supplies.find("text", {search:"\"chann\" \"mem\""}) 

對於上面的查詢我想顯示:

{ 
    TypeName" : "Not a Channel Member" 
} 

不過,我不能讓我的結果。

有什麼變化我在查詢做。 請幫幫我。

回答

0

下面的查詢將返回你想要的結果。

db.supplies.aggregate([ 
    {$unwind:"$TypeList"}, 
    {$match:{"TypeList.TypeName":{$regex:/.*chann.*mem.*/,$options:"i"}}}, 
    {$project:{_id:0, TypeName:"$TypeList.TypeName"}} 
]) 
+0

感謝....它的工作的罰款。 –

+0

如果我的集合包含多個對象,列表和字段,那麼查詢會是什麼?請幫助我。 –

0

如果你能接受以獲得類似這樣的輸出:

{ 
    "TypeList" : [ 
     { 
      "TypeName" : "Not a Channel Member" 
     } 
    ] 
} 

,那麼你可以避開使用聚合框架一般由運行下面的查詢有助於提高性能:

db.supplies.find(
{ 
    "TypeList.TypeName": /chann.*mem/i 
}, 
{ // project the list in the following way 
    "_id": 0, // do not include the "_id" field in the output 
    "TypeList": { // only include the items from the TypeList array... 
     $elemMatch: { //... where 
      "TypeName": /chann.*mem/i // the "TypeName" field matches the regular expression 
     } 
    } 
}) 

也看到此鏈接:Retrieve only the queried element in an object array in MongoDB collection

+0

謝謝.....正常工作。 –

+0

對於對象列表罰款...如果我的集合包含多個對象,列表和字段那麼將是什麼查詢?..請幫助我。 –

+0

請更具體一些,並給我們一些樣本數據,所需的輸入和輸出。 – dnickless