Array Queries in MongoDB

1

I have the following database

[
    {
        "codpeli": 1,
        "titol": "La La Land",
        "sinopsi": "Mia (Emma Stone), una solitaria aspirante a actriz y Sebastian (Ryan Gosling), un carismático aspirante a pianista de jazz, se enamoran en la ciudad de Los Angeles, una ciudad que les ha dado el amor, pero que también puede arrebatárselo. En una competición constante por buscar un hueco en el mundo del espectáculo, Mia y Sebastian descubren que el equilibrio entre el amor y el arte puede ser el mayor obstáculo de todos. ",
        "dvds": [2, 1, 3],
        "numprestecs": 2,
        "prestecs": [
            { "dataPres": ISODate("2017-05-28T11:32:18Z"), "coddvd": 3, "soci": "Maria", "dataDev": ISODate("2017-05-29T15:32:11Z") },
            { "dataPres": ISODate("2017-05-30T13:52:28Z"), "coddvd": 1, "soci": "Jordi" },
            { "dataPres": ISODate("2017-05-31T11:32:18Z"), "coddvd": 3, "soci": "Arnau" }
        ]
    },
    {
        "codpeli" : 2,
        "titol": "Figuras ocultas",
        "sinopsi": "Narra la historia nunca contada de tres brillantes mujeres científicas afroamericanas que trabajaron en la NASA a comienzos de los años sesenta (en plena carrera espacial, y asimismo en mitad de la lucha por los derechos civiles de los negros estadounidenses) en el ambicioso proyecto de poner en órbita al astronauta John Glenn.",
        "dvds": [5, 4],
        "numprestecs": 1,
        "prestecs": [ 
            { "dataPres": ISODate("2017-05-30T23:32:18Z"), "coddvd": 5, "soci": "Maria" }
        ]
    }
]

I need to list the (prestec) features that have not been returned (that do not have the 'dataDev' field) of the La La Land movie.

I do the query, but it only shows me the first one (number 1), when I should list two features:

db.videoclub.find(
            {
                titol : "La La Land",
                prestecs: { $elemMatch: { dataDev: { $exists : 0 }}}
            },
            {
                _id : 0,
                titol : 1,
                "prestecs.$" : 1
            }
        ).pretty()

results:

{
    "titol" : "La La Land",
    "prestecs" : [
        {
            "dataPres" : ISODate("2017-05-30T13:52:28Z"),
            "coddvd" : 1,
            "soci" : "Jordi"
        }
    ]
}
    
asked by Michael Salinas Rios 14.06.2017 в 12:10
source

1 answer

1

You can use aggregate() make $match first by titol and then $unwind to exploit the array and be able to redo $match on prestects.dataDev .

Then you can rearm the internal array with a $group and finally $project to show the keys you need:

db.videoclub.aggregate(
    { $match: { titol: "La La Land" } }, // Solo los que tienen titol "La La Land"
    { $unwind: "$prestecs" }, // "Aplana" el array "prestecs" 
    { $match: { "prestecs.dataDev": { $exists: 0 } } }, // Solo los que no tienen el campo prestecs.dataDev  
    { $group: { _id: "$titol", prestecs: { $addToSet: "$prestecs" } } }, //reagreupa el array prestecs
    { $project: { _id: 0, titol: "$_id", prestecs: "$prestecs" } } // Oculta el id y muestra titol y prestecs
).pretty()

This is the result obtained

{
    "titol" : "La La Land",
    "prestecs" : [
        {
            "dataPres" : ISODate("2017-05-31T11:32:18Z"),
            "coddvd" : 3,
            "soci" : "Arnau"
        },
        {
            "dataPres" : ISODate("2017-05-30T13:52:28Z"),
            "coddvd" : 1,
            "soci" : "Jordi"
        }
    ]
}
    
answered by 14.06.2017 / 17:09
source