vendredi 31 juillet 2015

Find() for embed document in mongodb

I have a mongodb for a photo albums. Some photos could be 'special'. So here is part of my db, a album:

{
        "_id" : ObjectId("55bc30befd401b12108b45a8"),
        "name" : "Vacation",
        "criate_at" : "2015-07-31",
        "photos" : [
                {
                        "uri" : "portfolio/h2171874c6e31bab44bed7df8c8ae91a.jpg",
                        "special" : true,
                        "criate_at" : "2015-07-31"
                },
                {
                        "uri" : "portfolio/o2171874c6e31bab44bed7df8c8ae91a.jpg",
                        "special" : false,
                        "criate_at" : "2015-07-31"
                },
                {
                        "uri" : "portfolio/o2171874c6e35bab44bed7df8c8aeq1a.jpg",
                        "special" : false,
                        "criate_at" : "2015-07-31"
                },
                {
                        "uri" : "portfolio/o2171874c6e31bab44bed7df8c8ae23a.jpg",
                        "special" : true,
                        "criate_at" : "2015-07-31"
                }
        ]
}
{
        "_id" : ObjectId("55bc30befd401b12108b4599"),
        "name" : "Zoo",
        "criate_at" : "2015-07-31",
        "photos" : [
                {
                        "uri" : "portfolio/h2171874c6e31bab44bed7df8c8ae933.jpg",
                        "special" : true,
                        "criate_at" : "2015-07-31"
                },
                {
                        "uri" : "portfolio/o2171874c6e31bab44bed7df8c8ae93f.jpg",
                        "special" : false,
                        "criate_at" : "2015-07-31"
                }
        ]
}

I want get only all special photos. I want this result:

{
        "_id" : ObjectId("55bc30befd401b12108b45a8"),
        "photos" : [
                {
                        "uri" : "portfolio/h2171874c6e31bab44bed7df8c8ae91a.jpg",
                        "special" : true,
                        "criate_at" : "2015-07-31"
                },
                {
                        "uri" : "portfolio/o2171874c6e31bab44bed7df8c8ae23a.jpg",
                        "special" : true,
                        "criate_at" : "2015-07-31"
                }
        ]
}
{
        "_id" : ObjectId("55bc30befd401b12108b4599"),
        "photos" : [
                {
                        "uri" : "portfolio/h2171874c6e31bab44bed7df8c8ae933.jpg",
                        "special" : true,
                        "criate_at" : "2015-07-31"
                }
        ]
}

I tried various queries, but nothing... This query give me the first special photos of each album.

db.albuns.find({'photos.special':true},{'photos':{$elemMatch:{'special':true}}}).pretty()

There is any way to get only all specials photos ?

Aucun commentaire:

Enregistrer un commentaire