使用MongoDB根据搜索条件提取特定数据字段

时间:2018-08-26 06:40:52

标签: java mongodb spring-data-mongodb

我是Spring Data MongoDB的新手,我想知道如何找到包含映射内部所有功能的所有模板。 类型:(例如“ 0E9994E9BC”和“ 479908E497”),如果我找到了它们,则返回此字段的模板ID和SID,而无需其他信息。

我尝试将聚合与Mongo $ project和$ filter一起使用,但是没有预期的结果。

示例

/* Template 1 */
{
     "_id" : "CDA1A44387",
     "name" : "template1",
     "mappings" : [
         {
             "sid" : "F1C153A770",
             "name" : "template1_field1",
             //doesnt contains any functional type
         },
         {
             "sid" : "EFC32CA865",
             "name" : "template1_field2",
             "functionalType" : "0E9994E9BC",
         }
     ],
}

/* Template 2 */
{
     "_id" : "DADFC6ADE0",
     "name" : "template2",
     "mappings" : [
         {
             "sid" : "0F59CAB24F",
             "name" : "template2_field1",
             "functionalType" : "479908E497", //ok
         },
         {
             "sid" : "6093B821A6",
             "name" : "template2_field2",
             "functionalType" : "0E9994E9BC", //ok
         },
         {
             "sid" : "ED97H82ED8",
             "name" : "template2_field3",
             "functionalType" : "0ED8SD8C88", //another functionalType
         }
     ],
}

/* Template 3 */
{
     "_id" : "30DC1DAFC8",
     "name" : "template3",
     "mappings" : [
         {
             "sid" : "3237AD97CF",
             "name" : "template3_field1",
             "functionalType" : "0E9994E9BC",
         },
         {
             "sid" : "5DE19A71DF",
             "name" : "template3_field2",
             //doesnt contains any functionalType
         },
         {
             "sid" : "EF7A58AD96",
             "name" : "template3_field3",
             "functionalType" : "479908E497",
         },
         {
             "sid" : "9HG9B55CV",
             "name" : "template3_field4",
             "functionalType" : "FIN5369DK2", //another functionalType
         },
     ],
}

结果

Template 1 - mappings didn't contain all of the search functional types, 
so we have to reject him

Template 2
{
 "_id" : "DADFC6ADE0",
 "mappings" : [
     {
         "sid" : "0F59CAB24F"
     },
     {
         "sid" : "6093B821A6"
     }
 ],
}

Template 3
{
 "_id" : "30DC1DAFC8",
 "mappings" : [
     {
         "sid" : "3237AD97CF"
     },
     {
         "sid" : "EF7A58AD96"
     }
 ],

0 个答案:

没有答案