显示mongoDB中嵌套文档字段值的顶级文档中的值



这是我想从中提取数据的文档

{
"_id" : ObjectId("635cbfee36945e4a79b0af70"),
"EID" : 200,
"EName" : "ram Kumar",
"ESalary" : 50000,
"EAddress" : {
"AddL1" : "fjwh fhwi fijwo",
"AddL2" : "uifwi efvhe",
"City" : "Blr",
"State" : "Karnataka",
"PinCode" : 560103,
"OfficeAddress" : {
"AddL1" : "bhr",
"State" : "Hyderabad",
"PinCode" : 125465,
"OfficeName" : "HCLN"
}
},
"EFevMovieDetails" : [
{
"MoveName" : "ROBO 1.0",
"WatchedDate" : "10/01/2022"
},
{
"MoveName" : "DDLJ",
"WatchedDate" : "01/01/2020"
},
{
"MoveName" : "Krish",
"WatchedDate" : "01/01/2015"
}
]
}

预期结果块

{
"_id" : ObjectId("635cbfee36945e4a79b0af70"),
"EID" : 200,
"EName" : "ram Kumar",
"ESalary" : 50000,
"EAddressState" : "Karnataka"
"EOfficeState" : "Hyderabad",
"EOfficePinCode" : 125465,
"EWantedToWatchAgainIfWeOffer" : "Krish" //MovieName must be available within the object "EFevMovieDetails"
}

将Model_name替换为Model和actual _id。

let data = await Model_name.findOne({_id: <Your _id>}).select({
"_id": 1,
"EID": 1,
"EName": 1,
"ESalary":1,
"EAddressState":"$EAddress.State",
"EOfficeState":"$EAddress.OfficeAddress.State",
"EOfficePinCode":"$EAddress.OfficeAddress.PinCode",
"EWantedToWatchAgainIfWeOffer":{"$last":"$EWantedToWatchAgainIfWeOffer.MoveName"}});

console.log(data)

相关内容

  • 没有找到相关文章

最新更新