2016-10-13 10 views
0

の配列で作業していないが、私のスキーマです:マングース移入がここたObjectId

/** Schemas */ 
var profile = Schema({ 
    EmailAddress: String, 
    FirstName: String, 
    LastName: String, 
    BusinessName: String 
}); 

var convSchema = Schema({ 
    name: String, 
    users: [{ 
     type: Schema.Types.ObjectId, 
     ref: 'Profiles' 
    }], 
    conversationType: { 
     type: String, 
     enum: ['single', 'group'], 
     default: 'single' 
    }, 
    created: { 
     type: Date, 
     default: Date.now 
    }, 
    lastUpdated: { 
     type: Date, 
     default: Date.now 
    } 
}); 

/** Models */ 
db.Profiles = mongoose.model('Profiles', profile); 
db.Conversations = mongoose.model('ChatConversations', convSchema); 

module.exports = db; 

それから私は、次のコード(http://mongoosejs.com/docs/populate.html)を使用してユーザーを移入しよう:

db.Conversations.find(query).populate('users').exec(function (err, records)  { 
    console.log(records); 
}); 

これはrecordsが、users配列を返します空白の配列[]としてください。

私はまた、他の方法で回避(http://mongoosejs.com/docs/api.html#model_Model.populate)を試してみました:

db.Conversations.find(query, function (err, records) { 
    db.Conversations.populate(records, {path: "users", select: "BusinessName"}, function (err, records) { 
     console.log(records); 
    }); 
}); 

結果は同じです。プロファイル収集レコードへの参照をチェックしたところ、そこにレコードがあります。

何が間違っていますか?

+0

すべてが正しいようです。私のために働いています。何も間違ったものが見えます – chirag

+0

どちらが働いたのですか? – Shreejibawa

答えて

3

私は(第三arguement)のモデルの名前を変更することにより、作業それを得た:問題はマングースがprofilesコレクションを探しますが、その存在し、データベース内のProfilesなどした

mongoose.model("Profiles", profile, "Profiles");

。だから正確な名前と一致するように名前をProfilesに変更しました。

Phewww!私に感謝します。

0

プロファイルスキーマは参照プロファイルを宣言しています。

ref: 'Profiles' 

var profile = Schema({ 
EmailAddress: String, 
FirstName: String, 
LastName: String, 
BusinessName: String 
}); 
+0

これらの2つの間には関係がありません。その単なる変数。 plsは詳細を私の答えをチェックします。 – Shreejibawa

関連する問題