如何在查找mongodb层次结构数据json中返回数组对象

时间:2015-02-14 19:22:42

标签: arrays json node.js mongodb

如何使用mongodb

返回数据库db.customers的数组对象“items”

我去另一个页面时遇到问题,因为查询返回一个对象,而不是对象数组项

Customer
        .find({user: req.user})
        .filter(filter)
        .order(sort)
        .map(function(doc){
              return doc.get('videos').items;
            })
        .page(pagination, function(err, customers){
                if(err){
                    return res.status(400).send({
                        message: errorHandler.getErrorMessage(err)
                    });
                } else{

                res.jsonp(customers);
                }
            });

在json结构下面,其中是数组项:

{
  "_id" : ObjectId("54de533a9585180c24f42887"),
  "videos" : {
    "items" : [{
        "snippet" : {
          "liveBroadcastContent" : "none",
          "thumbnails" : {

            "default" : {
              "height" : 90,
              "width" : 120,
              "url" : "https://i.ytimg.com/vi/xLWBnEQQiIA/default.jpg"
            }
          },
          "description" : "yea  foodporn",
          "title" : "#foodporn",
          "channelId" : "UC7FN6ofleSjbqwWxt-FNDWQ",
          "publishedAt" : "2015-01-16T03:21:35.000Z"
        },
        "id" : {
          "videoId" : "xLWBnEQQiIA",
          "kind" : "youtube#video"
        },
        "etag" : "\"k1sYjErg4tK7WaQQxvJkW5fVrfg/M4htbh5SyAjA8ul83DhltycTU6E\"",
        "kind" : "youtube#searchResult"
      }, {
        "snippet" : {
          "liveBroadcastContent" : "none",
          "thumbnails" : {

            "default" : {
              "height" : 90,
              "width" : 120,
              "url" : "https://i.ytimg.com/vi/v-1_pxe37-s/default.jpg"
            }
          },
          "description" : "food porn delicius",
          "title" : "#foodporn b",
          "channelId" : "UC7FN6ofleSjbqwWxt-FNDWQ",
          "publishedAt" : "2015-01-16T03:14:15.000Z"
        },
        "id" : {
          "videoId" : "v-1_pxe37-s",
          "kind" : "youtube#video"
        },
        "etag" : "\"k1sYjErg4tK7WaQQxvJkW5fVrfg/0SfjWbYvc4M7hdvXUFAsnZF-3vU\"",
        "kind" : "youtube#searchResult"
      }, {
        "snippet" : {
          "liveBroadcastContent" : "none",
          "thumbnails" : {

            "default" : {
              "height" : 90,
              "width" : 120,
              "url" : "https://i.ytimg.com/vi/DMfa1u8_3B8/default.jpg"
            }
          },
          "description" : "o yeah foos porn",
          "title" : "food porn a",
          "channelId" : "UC7FN6ofleSjbqwWxt-FNDWQ",
          "publishedAt" : "2015-01-16T03:03:09.000Z"
        },
        "id" : {
          "videoId" : "DMfa1u8_3B8",
          "kind" : "youtube#video"
        },
        "etag" : "\"k1sYjErg4tK7WaQQxvJkW5fVrfg/ghsCGgrP3VQw6kH3v2LijEPPUv8\"",
        "kind" : "youtube#searchResult"
      }],
    "pageInfo" : {
      "resultsPerPage" : 50,
      "totalResults" : 30
    },
    "nextPageToken" : "Cib3_b-JoP____96V0dSU3BHYjlCRQD_Af_-eldHUlNwR2I5QkUAARAeIbnxc1PpBEevOQAAAABfdkACSAFQAFoLCSuH7BJBzCiDEAE=",
    "etag" : "\"k1sYjErg4tK7WaQQxvJkW5fVrfg/WBVbC5oh7cdDE3IlyjlCZpMW7bQ\"",
    "kind" : "youtube#searchListResponse"
  },
  "user" : ObjectId("54de0f96b4d42d90195f892e"),
  "__v" : 0
}
  "user" : ObjectId("54de0f96b4d42d90195f892e"),
  "__v" : 0
}

2 个答案:

答案 0 :(得分:0)

Customer.find(...).map(function(doc){
  return doc.get('videos').items;
})

这就是你要找的东西?

答案 1 :(得分:0)

我认为您正在寻找的电话是

db.customers.find({ "user" : "xxx" }, { "videos.items" : 1 })

有关如何从查询中的文档返回特定字段的详细信息,请参阅projection docs