mongoose中的lookup有人用过吗?
GardenModel表
{ display: true,
itemList:
[ { type: 1, count: 100 },
{ type: 2, count: 99 },
{ type: 3, count: 50 } ],
_id: 5b2e0da2efa97927b06927ec,
__v: 0 }
UserModel表
{ _id: 5b2e0da2efa97927b06927ed,
nick: 'hehe',
gender: 1,
lastLogin: 2018-06-23T09:06:42.745Z,
garden: 5b2e0da2efa97927b06927ec,
__v: 0 }
查询如下:
async function test2(){
let docs = await UserModel.aggregate([
{
$lookup:{
from:"GardenModel",
localField:"garden",
foreignField:"_id",
as:"gardenInfo"
}
}
]);
console.log(docs[0]);
}
但是输出如下:不知道为啥gardenInfo是空的
{ _id: 5b2e0da2efa97927b06927ed,
nick: 'hehe',
gender: 1,
lastLogin: 2018-06-23T09:06:42.745Z,
garden: 5b2e0da2efa97927b06927ec,
__v: 0,
gardenInfo: [] }
当我用robo进行相同操作时如下
db.getCollection('usermodels').aggregate([
{
$lookup:{
from:"gardenmodels",
localField:"garden",
foreignField:"_id",
as:"gardenInfo"
}
}
])
{ “_id” : ObjectId(“5b2e52154c73cb22fc77b92c”), “nick” : “test”, “gender” : 1, “lastLogin” : ISODate(“2018-06-23T13:58:45.028Z”), “garden” : ObjectId(“5b2e52144c73cb22fc77b92b”), “__v” : 0, “gardenInfo” : [ { “_id” : ObjectId(“5b2e52144c73cb22fc77b92b”), “display” : true, “itemList” : [ { “type” : 1, “count” : 200 }, { “type” : 2, “count” : 99 }, { “type” : 3, “count” : 90 } ], “__v” : 0 } ] }