2013-05-17 68 views
0

在返回的某些元素使用下面的MongoDB查询:有没有办法只从一个数组的MongoDB文档

db.StyleHeader.find({ "FlexContent.ExtendedContent" : { "$elemMatch" : { "Defines" : "Old Keywords" } } }, { "FlexContent.ExtendedContent.Defines" : true, "FlexContent.ExtendedContent.Value" : true }).limit(50); 

我得到如下结果:

{ 
    "_id" : 1331142, 
    "FlexContent" : { 
    "ExtendedContent" : [{ 
     "Defines" : "Content Keywords", 
     "Value" : "FST-SET, SIZING_CHART-WOMENS_SIZING" 
     }, { 
     "Defines" : "Search Keywords", 
     "Value" : "Closeout, Sale, Tops, T-Shirt, Womens" 
     }, { 
     "Defines" : "Exceptions", 
     "Value" : "" 
     }, { 
     "Defines" : "Old Keywords", 
     "Value" : "1020A20,PRT,SML,Let's,Tour,W.,S9S,T,Shirt,By,Twin,Six,#1C,PRINT,SM,1020A20PRTSML,WEBSORTZ,CAT-TOPS-WOMENS-ALL,1020898ZZ,1010A17ZZ,1020A21ZZ,1020902ZZ,1020904ZZ,CAT-TWINSIX,CAT-GIFTIDEAS,CAT-GIFTS-30,CAT-GIFTS-50,1020903ZZ,CAT-GIFTS-NEW,CAT-TOPS-WOMENS-TSHRT,CAT-GIFTS-HER,CAT-TOPS-TSHIRT,SIZING_CHART-WOMENS_SIZING,CAT-GIFTS-ALL,FST.-SET,FGT.-TSHIRT,CAT-SALE-ALL,CAT-SALE-TOPS,ZKQX,CO_01,DIV_00,CAT1_BA,CAT2_AC" 
     }, { 
     "Defines" : "Name", 
     "Value" : "Let's Tour Women's Short Sleeve T Shirt By Twin Six " 
     }, { 
     "Defines" : "Base Item", 
     "Value" : "1020A20 PRT SML" 
     }, { 
     "Defines" : "Price", 
     "Value" : "21.95" 
     }, { 
     "Defines" : "Upsells", 
     "Value" : "1010A17, 1020898, 1020902, 1020903, 1020904, 1020A21" 
     }, { 
     "Defines" : "Default Customization Template", 
     "Value" : "not applicable" 
     }, { 
     "Defines" : "Product Description Style", 
     "Value" : "None" 
     }, { 
     "Defines" : "Promo Search", 
     "Value" : "" 
     }, { 
     "Defines" : "Auto Customization", 
     "Value" : "" 
     }] 
    } 
} 

我真的只需要ExtendedContent定义了“搜索关键字”。

是否有修改我的查询这样的结果看起来更加的方式是这样的:

{ 
    "_id" : 1331142, 
    "FlexContent" : { 
    "ExtendedContent" : [{ 
     "Defines" : "Search Keywords", 
     "Value" : "Closeout, Sale, Tops, T-Shirt, Womens" 
     }] 
    } 
} 

我试图找出查询结构首先在MongoVUE但最终这将被用做C#驱动程序,所以在任何一个解决方案将为我工作。

回答

2

可以使用$ positional projection operator做到这一点:

db.StyleHeader.find(
    { "FlexContent.ExtendedContent" : { 
     "$elemMatch" : { "Defines" : "Old Keywords" } 
    } }, 
    { "FlexContent.ExtendedContent.$" : true } 
).limit(50); 

甚至有点简单(因为你只在ExtendedContent数组元素匹配一个字段):

db.StyleHeader.find(
    { "FlexContent.ExtendedContent.Defines" : "Old Keywords" }, 
    { "FlexContent.ExtendedContent.$" : true } 
).limit(50); 
相关问题