Mongodb $ lookup不能使用_id

我们试着用这个查询,返回查找是空的

db.getCollection('tests').aggregate([ {$match: {typet:'Req'}}, {$project: {incharge:1}}, {$lookup:{ from: "users", localField: "incharge", //this is the _id user from tests foreignField: "_id", //this is the _id from users as: "user" }} ]) 

返回json

  [ { "_id": "57565d2e45bd27b012fc4db9", "incharge": "549e0bb67371ecc804ad23ef", "user": [] }, { "_id": "57565d2045bd27b012fc4cbb", "incharge": "549e0bb67371ecc804ad21ef", "user": [] }, { "_id": "57565d2245bd27b012fc4cc7", "incharge": "549e0bb67371ecc804ad24ef", "user": [] } ] 

我尝试这个post,但没有任何发生MongoDB聚合项目string到ObjectId和与_id这个MongoDB $ lookup作为一个foreignField在PHP

UPDATE

这是Document“用户”

  { "_id" : ObjectId("549e0bb67371ecc804ad24ef"), "displayname" : "Jhon S." }, { "_id" : ObjectId("549e0bb67371ecc804ad21ef"), "displayname" : "George F." }, { "_id" : ObjectId("549e0bb67371ecc804ad23ef"), "displayname" : "Franc D." } 

我finalyfind了解决scheme,是与我的架构与mongoose与ObjectId的问题

我改变这一点

 var Schema = new Schema({ name: { type: String, required: true}, incharge: { type: String, required: true}, }); 

有了这个

 var Schema = new Schema({ name: { type: String, required: true}, incharge: { type: mongoose.Schema.ObjectId, required: true}, }); 

并正在工作

你的查询查询是完美的,但问题是你正在将string存入数据库,而_id:ObjectId('theID')是一个对象,而不只是string,你不能比较string ('') ({})。 所以,最好的方法是将增量键存储为一个对象( mongoose.Schema.ObjectId ),而不是作为模式中的string。