return only 1 element of an array

2019-08-19 06:48发布

I have a mongodb collection structured like this:

/* 1 */
{
  "_id" : ObjectId("551fc02d26a8a48c32000029"),
  "nome" : "ist1",
  "username" : "istituzione1",
  "email" : "some@email.it",
  "pwd" : "0cc175b9c0f1b6a831c399e269772661",
  "punti" : [{
      "punto_id" : ObjectId("551fc0ca26a8a48c3200002b"),
      "youtubelink" : "",
      "immagini" : [{
          "name" : "iis-8.png",
          "imgid" : ObjectId("551fc17426a8a48c3200002f"),
          "contenttype" : "image/png"
        }, {
          "name" : "iis-85.png",
          "imgid" : ObjectId("551fc17526a8a48c32000031"),
          "contenttype" : "image/png"
        }]
    }, {
      "punto_id" : ObjectId("551fc0d226a8a48c3200002c"),
      "youtubelink" : "",
      "immagini" : [{
          "name" : "bkg-blu.jpg",
          "imgid" : ObjectId("551fc1be26a8a48c32000033"),
          "contenttype" : "image/jpg"
        }, {
          "name" : "w-brand.png",
          "imgid" : ObjectId("551fc1bf26a8a48c32000036"),
          "contenttype" : "image/png"
        }]
    }]
}

/* 2 */
{
  "_id" : ObjectId("551fc09e26a8a48c3200002a"),
  "nome" : "ist2",
  "username" : "istituzione2",
  "email" : "some@email.it",
  "pwd" : "92eb5ffee6ae2fec3ad71c777531578f",
  "punti" : [{
      "punto_id" : ObjectId("551fc11e26a8a48c3200002d"),
      "youtubelink" : "",
      "immagini" : [{
          "name" : "gnagna non ha fatto ridere.jpg",
          "imgid" : ObjectId("551fc20226a8a48c32000038"),
          "contenttype" : "image/jpg"
        }, {
          "name" : "prova.jpg",
          "imgid" : ObjectId("551fc20226a8a48c3200003a"),
          "contenttype" : "image/jpg"
        }]
    }, {
      "punto_id" : ObjectId("551fc12326a8a48c3200002e"),
      "youtubelink" : "",
      "immagini" : [{
          "name" : "gnana meme 2.jpg",
          "imgid" : ObjectId("551fc22426a8a48c3200003c"),
          "contenttype" : "image/jpg"
        }, {
          "name" : "dogeminer finito store.png",
          "imgid" : ObjectId("551fc22626a8a48c3200003e"),
          "contenttype" : "image/png"
        }]
    }]
}

I'm trying to do this query to return a single element in the immagini array:

db.istituzioni.find({_id:ObjectId("551fc02d26a8a48c32000029"), 'punti.punto_id': ObjectId("551fc0d226a8a48c3200002c"), 'punti.immagini.imgid':ObjectId("551fc17426a8a48c3200002f")}, { "punti.immagini.$" : 1 })

It return the correct selection on the fields _id and punti.punto_id, but it return all the elements of the array immagini.
I thought that with { "punti.immagini.$" : 1 }, the query would have returned the first element of the immagini array that I needed.
How can I make this query return only one element of the array immagini?

1条回答
时光不老,我们不散
2楼-- · 2019-08-19 07:12

Use following query

db.istituzioni.find({_id:ObjectId("551fc02d26a8a48c32000029"), 'punti.punto_id': ObjectId("551fc0d226a8a48c3200002c"), 'punti.immagini.imgid':ObjectId("551fc17426a8a48c3200002f")}, {_id: 0, 'punti.immagini.$': 1}   ).forEach(function(myDoc) {
        if (myDoc._id.toString() === '551fc02d26a8a48c32000029') {
            var imagArr = myDoc.punti.immagini;
            if (imagArr.imgid.toString() === '551fc17426a8a48c3200002f') {
                //do here what you want
            }
        }
});

Thanks

查看更多
登录 后发表回答