These are chat archives for Automattic/mongoose

30th
Oct 2016
temuccio
@temuccio
Oct 30 2016 08:45
Hello all
temuccio
@temuccio
Oct 30 2016 09:52
I have one big problem
I have this database:
{
    "_id" : ObjectId("57dc2b63add5c5e91f185fe0"),
    "level" : 2,
    },
    "profili" : [ 
        {
            "_id" : ObjectId("507f1f77bcf86cd799439011"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
            "costopag" : 1,
            "pagmax" : 10,
            "pagmin" : 1
        }, 
        {
            "_id" : ObjectId("507f191e810c19729de860ea"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
            "costopag" : 0.5,
            "pagmax" : 100,
            "pagmin" : 11
        }
    ],
    "__v" : 0
}

{
    "_id" : ObjectId("57dc39945aec26b303053dd7"),
    "level" : 2,
    },
    "profili" : [ 
        {
            "_id" : ObjectId("507f1f77bcf86cd799439012"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
            "costopag" : 10,
            "pagmax" : 100,
            "pagmin" : 11
        }
    ],
}

{
    "_id" : ObjectId("57e7ff05a0ef6d07c3048d7f"),
    "level" : 2,
    },
    "profili" : [ 
        {
            "_id" : ObjectId("507f1f77bcf86cd799439013"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498c"),
            "costopag" : 0.5,
            "pagmax" : 10,
            "pagmin" : 1
        }
    ],
}

{
    "_id" : ObjectId("57e7ff54a0ef6d07c3048d81"),
    "level" : 2,
    },
    "profili" : [ 
        {
            "_id" : ObjectId("507f1f77bcf86cd799439014"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498c"),
            "costopag" : 5,
            "pagmax" : 10,
            "pagmin" : 1
        }
    ],
    "__v" : 0
}
If I execute this find:
temuccio
@temuccio
Oct 30 2016 09:57
database.Utente.find({
          "level":2,
          profili: {
            $elemMatch:{
              idprofilo: '57fe3c51569b5c0afb88498b',
              pagmin: {$lte:2},
              pagmax: {$gte:2}
            }
          }
        })
The results return
{
    "_id" : ObjectId("57dc2b63add5c5e91f185fe0"),
    "level" : 2,
    },
    "profili" : [ 
        {
            "_id" : ObjectId("507f1f77bcf86cd799439011"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
            "costopag" : 1,
            "pagmax" : 10,
            "pagmin" : 1
        }, 
        {
            "_id" : ObjectId("507f191e810c19729de860ea"),
            "idprofilo" : ObjectId("57fe3c51569b5c0afb88498b"),
            "costopag" : 0.5,
            "pagmax" : 100,
            "pagmin" : 11
        }
    ],
    "__v" : 0
}
I search, if it is possible, that the results has only first profile and not the second profile....Is it possible?
antonioaltamura
@antonioaltamura
Oct 30 2016 12:43
do you want to hide in result the second element of the array "profili" of each document? In this case I guess you have to use Aggregation and project.
temuccio
@temuccio
Oct 30 2016 14:01
hello @antonioaltamura
Yes this is my attend
I have try with aggregation and project, but I don't understand how to use it