2013-08-04 1 views
0

포함 된 문서 필드를 필터링하는 정적 쿼리를 정의 할 때 연결 관련 오류가 발생합니다. 별도의 스키마 파일에서 포함 된 문서를 분리하려고 시도했지만 문제가 해결되지 않았습니다. 어떤 아이디어?몽구스 및 포함 된 문서를 사용하여 '닫히지 않은 연결을 열려고합니다'오류가 발생했습니다.

오류는 다음과 같습니다 필터 다음 모델에서 { text.lang_code: langCode } 옵션을 사용할 때

C:\development_GIT\myproject\app\models\mymodel.js:40 
    this.find({ text.lang_code: langCode }).sort('text.name').exec(callback); 
         ^

Error: Trying to open unclosed connection. 
    at NativeConnection.Connection.open (C:\development_GIT\myproject\node_ 
modules\mongoose\lib\connection.js:205:15) 
    at Mongoose.connect (C:\development_GIT\myproject\node_modules\mongoose 
\lib\index.js:156:15) 
    at Object.<anonymous> (C:\development_GIT\myproject\server.js:13:10) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.require (module.js:364:17) 
    at require (module.js:380:17) 
    at repl:1:1 

오류가 시작됩니다. 임베디드 문서를 사용하지 않고 예제 { _id: langCode }으로 필터링하려고 시도하면 오류가 발생하지 않습니다. 내 주요 파일 server.js의

//MyModel.js located at ./app/models 

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



    var MyModelSchema = new Schema({ 
     name: { type: String, trim: true }, 
     text: [{ name: String, lang_code: String }] 

    }); 


    MyModelSchema .static({ 


     findByLangCode : function(langCode, callback) { 

     this.find({ text.lang_code: langCode }).sort('text.name').exec(callback); 

     } 

    }); 

mongoose.model('MyModel', CategorySchema); 

첫 번째 라인은 다음과 같습니다

//server.js 
var express = require('express'); 
var env = process.env.NODE_ENV || 'development'; 
var config = require('./config/config')[env]; 
var mongoose = require('mongoose'); 
var fs = require('fs'); 
require('express-namespace'); 

mongoose.connect(config.db); 

// Bootstrap models 
fs.readdirSync(__dirname + '/app/models').forEach(function (file) { 
    if (~file.indexOf('.js')) require(__dirname + '/app/models/' + file) 
}); 

답변

0

솔루션은 다른 방법으로 쿼리를 구축했다. find() 내에서 하위 문서를 사용할 수없는 것 같습니다.

Before: (not working) 
this.find({ text.lang_code: langCode }).sort('text.name').exec(callback); 

After (working) 

this.find().where('text.lang_code').equals(langCode).sort('text.name').exec(callback); 
0

메신저 내 모든 시간이 사용하고 그것은 나를 위해 잘 작동합니다.

this.find({ 'text.lang_code': langCode }).sort('text.name').exec(callback);

MongoDB를 만이 어디에요 기능에서하고있는 것처럼 당신이 그것을 문자열을 주면 있지만, MongoDB를 마법 할 것, 개체 중 하나 LVL을 처리하고 하위 문서 :

+0

덕분에 일치시킬 수 있습니다 둘러보기에 대한 의견. 특정 조건에서 예상대로 작동한다는 것을 알면 좋습니다. 어쩌면 내 코드와 내 코드의 차이점은 어떻게 연결이 관리되는지 ... – Endymion

관련 문제