Saturday 6 May 2017

how to query array population mongoose

./models/info-hotspot.js

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

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

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

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

./models/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);

Query

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

All of its return infoHotspots: [ [Object], [Object]] (Just work without array)

What's the reason? How to solve this problem? Thanks for helping!



via Clavis

No comments:

Post a Comment