How to return just the matched elements from a mon

2019-05-06 06:33发布

问题:

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

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"
              }
            ]
}

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

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.

This is the query I'm using:

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"
  }]
}

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.

Thank you so much!

回答1:

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.

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

$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
         }
     }
])

will produce:

{
    "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
}