如何查询数组人口mongoose

时间:2017-05-06 15:42:46

标签: node.js mongodb mongoose

./模型/信息-hotspot.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var Scene = require('../models/scene');

var schemaInfoHotspot = new Schema({
  scene: { type: Schema.Types.ObjectId, ref: 'Scene', required: true },
  title: { type: String, require: false }
});

module.exports = mongoose.model('InfoHotspot', schemaInfoHotspot); 

./模型/ scene.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var InfoHotspot = require('../models/info-hotspot');

var schemaScene = new Schema({
    name: { type: String, required: true },
    infoHotspots: [
        {
            type: Schema.Types.ObjectId, ref: 'InfoHotspot',
            required: false
        }
    ]
});

module.exports = mongoose.model('Scene', schemaScene);

查询

  Scene
  .find()
  //.populate('infoHotspots')
  //.populate({
    //path: 'infoHotspots',
    //model: 'InfoHotspot'
  //})
  .exec(function(err, doc){
    if(err) return console.log(err);
    else  console.log(doc);
  });

所有返回infoHotspots:[[Object],[Object]] (只是没有数组工作)

原因是什么? 如何解决这个问题?

我想要这个结果:

 Scenes = {
              "name": "scene-1",
              "infoHotspots": [
                {
                  "title": "my title1"
                },
                {
                  "title": "my title2"
                },
                {
                  "title": "my title3"
                }
              ]
         }

感谢您的帮助!

0 个答案:

没有答案