如何返回刚刚从MongoDB的数组中的元素相匹配 [英] How to return just the matched elements from a mongoDB array

查看:406
本文介绍了如何返回刚刚从MongoDB的数组中的元素相匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题的一个星期,我不明白为什么它仍然不工作...

I've been looking for this question one week and I can't understand why it still don't work...

我有这个对象到MongoDB的我的数据库:

I have this object into my MongoDB database:

{
        produc: [
              {
                cod_prod: "0001",
                description: "Ordenador",
                price: 400,
                current_stock: 3,
                min_stock: 1,
                cod_zone: "08850"
              },
              {
                cod_prod: "0002",
                description: "Secador",
                price: 30,
                current_stock: 10,
                min_stock: 2,
                cod_zone: "08870"
              },
              {
                cod_prod: "0003",
                description: "Portatil",
                price: 500,
                current_stock: 8,
                min_stock: 4,
                cod_zone: "08860"
              },
              {
                cod_prod: "0004",
                description: "Disco Duro",
                price: 100,
                current_stock: 20,
                min_stock: 5,
                cod_zone: "08850"
              },
              {
                cod_prod: "0005",
                description: "Monitor",
                price: 150,
                current_stock: 0,
                min_stock: 2,
                cod_zone: "08850"
              }
            ]
}

我想查询数组元素具有特定cod_zone(08850)为例。

I would like to query for array elements with specific cod_zone ("08850") for example.

我发现$ elemMatch投影,理应应该只返回匹配查询该数组元素,但我不知道为什么我得到的所有对象。

I found the $elemMatch projection that supposedly should return just the array elements which match the query, but I don't know why I'm getting all object.

这是我使用的查询:

db['Collection_Name'].find(
    {
        produc: {
            $elemMatch: {
                cod_zone: "08850"
            }   
        }
    }
);

这是我期待的结果:

And this is the result I expect:

{ produc: [
  {
    cod_prod: "0001",
    denominacion: "Ordenador",
    precio: 400,
    stock_actual: 3,
    stock_minimo: 1,
    cod_zona: "08850"
  },{
    cod_prod: "0004",
    denominacion: "Disco Duro",
    precio: 100,
    stock_actual: 20,
    stock_minimo: 5,
    cod_zona: "08850"
  },
  {
    cod_prod: "0005",
    denominacion: "Monitor",
    precio: 150,
    stock_actual: 0,
    stock_minimo: 2,
    cod_zona: "08850"
  }]
}

我正在使用MongoDB的Java连接器的Java程序,所以我真的需要Java连接器的查询,但我认为我将能够得到它,如果我知道蒙戈查询。

I'm making a Java program using MongoDB Java Connector, so I really need a query for java connector but I think I will be able to get it if I know mongo query.

感谢你了!

推荐答案

这是有可能通过在聚合框架 。该管道通过集合中的所有文件通过如下操作:

This is possible through the aggregation framework. The pipeline passes all documents in the collection through the following operations:

$放松 运营商 - 输出一个文档在全国生产数组字段的每个元素通过解构它

$unwind operator - Outputs a document for each element in the produc array field by deconstructing it.

$匹配 运营商将仅过滤文档匹配 cod_zone 标准。​​

$match operator will filter only documents that match cod_zone criteria.

$组 运营商将通过集团指定的标识符前pression输入文件,并且使用蓄能前pression的 $推 每组:

$group operator will group the input documents by a specified identifier expression and applies the accumulator expression $push to each group:

$项目 运营商则重建流中的每个文件:

$project operator then reconstructs each document in the stream:

db.collection.aggregate([
    { 
        "$unwind": "$produc" 
    },
    {
        "$match": {
            "produc.cod_zone": "08850"
        }
    },
    {
       "$group":
         {
           "_id": null,
           "produc": { 
               "$push":  { 
                    "cod_prod": "$produc.cod_prod", 
                    "description": "$produc.description",
                    "price" : "$produc.price",
                    "current_stock" : "$produc.current_stock",
                    "min_stock" : "$produc.min_stock",
                    "cod_zone" : "$produc.cod_zone" 
               } 
            }
         }
     },
     {
         "$project": {
             "_id": 0,
             "produc": 1
         }
     }
])

会产生:

{
    "result" : [ 
        {
            "produc" : [ 
                {
                    "cod_prod" : "0001",
                    "description" : "Ordenador",
                    "price" : 400,
                    "current_stock" : 3,
                    "min_stock" : 1,
                    "cod_zone" : "08850"
                }, 
                {
                    "cod_prod" : "0004",
                    "description" : "Disco Duro",
                    "price" : 100,
                    "current_stock" : 20,
                    "min_stock" : 5,
                    "cod_zone" : "08850"
                }, 
                {
                    "cod_prod" : "0005",
                    "description" : "Monitor",
                    "price" : 150,
                    "current_stock" : 0,
                    "min_stock" : 2,
                    "cod_zone" : "08850"
                }
            ]
        }
    ],
    "ok" : 1
}

这篇关于如何返回刚刚从MongoDB的数组中的元素相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆