2017-02-06 5 views
0

を返しません私のノードアプリケーションと一緒にMongoose version ^4.8.1を使用しています。私は、次のようにしている上記の文書のための3つのスキーマモデル、model.find({})、</p> <p><a href="https://i.stack.imgur.com/NvBXX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NvBXX.png" alt="enter image description here"></a></p> <p>Iを( '場所')を移入移入( '位置')私はMongoDBの上に以下の構造を有する文書を有する場所と場所

Event.js

var mongoose = require('mongoose'); 
var eventSchema = new mongoose.Schema({ 
    description: { 
     type: String   
    }, 
    end_time: { 
     type: Date  
    }, 
    start_time: { 
     type: Date  
    }, 
    name: { 
     type: String  
    }, 
    place: { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'place' 
    } 
}); 
eventSchema.index({name: 'text'},{'place.location.country':"text"}); 
var Event = mongoose.model('events', eventSchema); 
module.exports= Event; 

Place.js

var mongoose = require('mongoose'); 
var placeSchema = new mongoose.Schema({ 
    name: { 
     type: String  
    }, 
    location: { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'location' 
    } 
}); 

var Place = mongoose.model('place', placeSchema); 
module.exports= Place; 

Location.js

を作成しました
var mongoose = require('mongoose'); 
var locationSchema = new mongoose.Schema({ 
    city: { 
     type: String   
    }, 
    latitude: { 
     type: String   
    }, 
    country: { 
     type: String   
    }, 
    located_in: { 
     type: String   
    }, 
    state: { 
     type: String   
    }, 
    street: { 
     type: String   
    }, 
    zip: { 
     type: String   
    }, 


}); 
var Location = mongoose.model('location', locationSchema); 
module.exports= Location; 
アクセス/クエリデータベースへ

共通のハンドラ、

dbHandler.jsここ

querandpoplulate : function(model,condition,options) 
    { 
     return new Promise(function(resolve, reject) { 
      options = options||{}; 
      console.log("model is" + model); 
      model.find({}).populate('place').populate('location').exec(function(error, data) { 
       if (error) 
        console.log(error); 
        reject(error);   
       console.log(data); 
       resolve(data); 
      }) 
     }) 

    } 

iが照会方法です、

dbHelper.querandpoplulate(mongoose.model('events'), {$text: {$search: searchString},'place.location.country': countryString},function(error,data){ 
          callback(data);  
}); 

質問:それは結果セットを返しません場所と場所を指定すると、nullが返されます。

答えて

1

文書はembedded documentsとして保存されていますが、referenced documentsとして保存されていないようです。

このようなドキュメントを取得するには、​​を実行する必要はありません。単純なfindクエリはあなたのために働くはずです。

はこれを試してみてください:

model.find({}).exec(function(error, data) { 
    if (error) 
     console.log(error); 
     reject(error);   
    console.log(data); 
    resolve(data); 
}) 

あなたはmongoDBdataを保存するだけで、それを取得していないと。 document構造に一致するschemaを定義する必要があります。

あなたと一緒に説明したように、私はあなたがSchemaを変更し、一つのファイル(Event.js)内のすべての3 schemasを結合する必要があると思います。

var mongoose = require('mongoose'); 
var locationSchema = new mongoose.Schema({ 
    city: { 
     type: String   
    }, 
    //add other fields here 
}); 

var placeSchema = new mongoose.Schema({ 
    name: { 
     type: String  
    }, 
    location: locationSchema 
}); 

var eventSchema = new mongoose.Schema({ 
    description: { 
     type: String   
    }, 
    //add other fields here too 
    place: placeSchema 
}); 
eventSchema.index({name: 'text'},{'place.location.country':"text"}); 
var Event = mongoose.model('events', eventSchema); 
module.exports= Event; 
+0

はまだそれが場所を返します。これは結果である – Sajeetharan

+0

ヌル{_id:5890b47f6166c457ffdee3d3、 END_TIME:2016-11-11T00:00:00.000Z、 名: 'エネルギアツアーエウロパ - ミラノ'、 場所: null、 start_time:2016-11-10T19:00:00.000Z、 id: '631789367000065'} – Sajeetharan

+0

あなたのインデックス 'place.location.country ':" text "'は間違っているようですが、placeはそのスキーマのobjectIdです、place.locationは未定義です。私はこれが問題を引き起こすかどうか分からないが、他のすべてはうまく見える –

関連する問題

 関連する問題