仅返回1数组元素 [英] return only 1 element of an array

查看:242
本文介绍了仅返回1数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合的Mon​​goDB的结构是这样的:

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

我试图做到这一点查询返回在一个单一的元素 immagini 数组:

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

它返回的字段 _id punti.punto_id 正确的选择,但它返回的所有元素数组 immagini 。结果我想与 {punti.immagini $:1} ,查询将返回的 immagini的第一个元素数组,我需要的。结果
我怎样才能让这个查询返回数组只有一个元素 immagini

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?

推荐答案

使用下面的查询

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
            }
        }
});

感谢

这篇关于仅返回1数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆