Mongodb $ lookup不适用于_id [英] Mongodb $lookup Not working with _id
本文介绍了Mongodb $ lookup不适用于_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用此查询,返回查找为空
wend try with this query, return the lookup is empty
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": []
}
]
我尝试了这篇文章,但没有任何反应 将MongoDB聚合项目字符串转换为ObjectId 并与此 以_id作为PHP中的ForeignField的MongoDB $ lookup
i try with this post but nothing happend MongoDB aggregation project string to ObjectId and with this MongoDB $lookup with _id as a foreignField in PHP
更新
这是文档的用户"
{
"_id" : ObjectId("549e0bb67371ecc804ad24ef"),
"displayname" : "Jhon S."
},
{
"_id" : ObjectId("549e0bb67371ecc804ad21ef"),
"displayname" : "George F."
},
{
"_id" : ObjectId("549e0bb67371ecc804ad23ef"),
"displayname" : "Franc D."
}
推荐答案
我最终找到了解决方案,这是我的模式在带有ObjectId的猫鼬模式中的问题
I finaly found the solution, is a problem with my Schema in mongoose with the 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},
});
正在工作
这篇关于Mongodb $ lookup不适用于_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文