Mongodb with Node.js如何从子文档查询数据

Mongodb with Node.js如何从子文档查询数据,node.js,mongodb,Node.js,Mongodb,我试图弄清楚如何获取“openingTime”模式的数据,它是“location”模式中的一个子文档 然后当我尝试查询数据时 var mongoose = require('mongoose'); var Loc = mongoose.model('Location'); /* GET a location by the id */ module.exports.locationsReadOne = function(req, res) { // begin request to retur

我试图弄清楚如何获取“openingTime”模式的数据,它是“location”模式中的一个子文档

然后当我尝试查询数据时

var mongoose = require('mongoose');
var Loc = mongoose.model('Location');

/* GET a location by the id */
module.exports.locationsReadOne = function(req, res) {
 // begin request to return single location information
 // received from app_server's getLocationInfo()
  var result = [];

  if (req.params && req.params.locationid) {
      Loc
        .findById(req.params.locationid)
        .exec(function(err, location) {
          if (!location) {
            sendJSONresponse(res, 404, {
              "message": "locationid not found"
            });
            return;
          } else if (err) {
            console.log(err);
            sendJSONresponse(res, 404, err);
            return;
          }

          console.log("\n\n about to try to query open time id: ");
          console.log(location.openingTimes[0]);
          console.log("\n\n\n");

          try{ 
            result.push({

              rating: location.rating,              
              facilities: location.facilities,
              _id: location._id,
              name: location.name,
              address: location.address,
              coords: location.coords,
              openingTimes: location.openingTimes,
              reviews: location.reviews,
              __v: location.__v
            });
          }
          catch(e){
              console.log("Did not find object:  ending loop.  "+e);
          } 

          console.log(result);
          sendJSONresponse(res, 200, result);
        });
      }
      else {
        console.log('No locationid specified');
        sendJSONresponse(res, 404, {
          "message": "No locationid in request"
        });
      }
  };
我得到以下输出:

About to try to query open time id:
{ _id: 5b6f26b7a0232b6e907d1c42 }


[ { rating: 0,
    facilities: ["Hot drinks","Food","Clean Bathroom","Some sitting"],
    _id: 5b6f26b7a0232b6e907d1c40,
    name: 'Peets Coffee',
    address: '200 Park Street, Marcone, IL 94872',
    coords: [-322.2991,37.4096],
    openingTimes: CoreMongooseArray [ [Object], [Object] ],
    reviews: CoreMongooseArray [],
    __v: 0 } ]

如何获取“OpeningTime”数组中的实际数据?我尝试使用,.populate('openingTime'),它只给了我一个_id

你的“openingTime”数组有一个实际的数据,当你打印数据时,你可以使用
console.log(JSON.stringify(result))

底线是,尽管我创建了一个子文档,并在插入新文档时获得了一个id,子文档数据未保存。我需要更新父文档并找到一种方法来保存子文档数据。以下是我如何发现问题的:

Loc
      .findById(req.params.locationid)
      .select('openingTimes')  //you can also try to populate()
      .exec(function(err, location) {
        console.log("\n\n");
        console.log("Length of opening times array is: "+location.openingTimes.length);
        var thisOpenTime = location.openingTimes.id(req.params.reviewid);             
        console.log("\nThe actual data is: "+thisOpenTime);
        console.log("\n\n");
      });
正在执行findById().select('column').exec(…)

GET/api/locations/5b6f26b7a0232b6e907d1c40 200 80.179 ms-320

//产出如下:

开放时间数据为: [{u id:'5b6f26b7a0232b6e907d1c42'}, {{u id:'5b6f26b7a0232b6e907d1c41'}] 获取/定位/5b6f26b7a0232b6e907d1c40/500 632.998 ms-3621

“开放时间数组的长度为:2

实际数据为:null“


因为,我得到了一个空值,这意味着子文档中没有数据。阵列。

运气不好!在stringify之后,我继续获取“\u id”值。。。。请参阅下面粘贴的要尝试查询开放时间id的输出:{“_id”:“5b6f26b7a0232b6e907d1c42”}以下是输出上述值的代码:var opentimedata=JSON.stringify(location.openingTimes[0]);log(“\n用于尝试查询打开时间id:”);console.log(opentimedata);控制台日志(“\n”);
Loc
      .findById(req.params.locationid)
      .select('openingTimes')  //you can also try to populate()
      .exec(function(err, location) {
        console.log("\n\n");
        console.log("Length of opening times array is: "+location.openingTimes.length);
        var thisOpenTime = location.openingTimes.id(req.params.reviewid);             
        console.log("\nThe actual data is: "+thisOpenTime);
        console.log("\n\n");
      });