I have a collection that has a sub-document in it and I'm trying to find an easy way to get an exact document.
{
"_id" : ObjectId("59b0303bfe409a21fccc9523"),
"CreatedOn" : ISODate("2017-07-22T15:55:00.000+0000"),
"UpdatedOn" : ISODate("2017-07-22T15:55:00.000+0000"),
"Guid" : "4F1D7541-FF27-4FEF-9BC4-CF27D2CB92BA",
"Company" : "Demo Co.",
"Departments" : [
{
"CreatedOn" : ISODate("2017-07-22T15:55:00.000+0000"),
"UpdatedOn" : ISODate("2017-07-22T15:55:00.000+0000"),
"Guid" : "D5950FC8-91B1-48A3-8B6A-F3A72FAC9175",
"Title" : "Execute"
}
]
}
what I'd like to do is get a certain department based on its guid but so far I'm not finding anything that is clean to get just the document I want.
I'd want to get a result like this:
{
"CreatedOn" : ISODate("2017-07-22T15:55:00.000+0000"),
"UpdatedOn" : ISODate("2017-07-22T15:55:00.000+0000"),
"Guid" : "D5950FC8-91B1-48A3-8B6A-F3A72FAC9175",
"Title" : "Execute"
}
I've been looking around the mongodb documentation but nothing is working so far, the closest I'm getting is using array_search, but I'd like to fetch it straight from mongo itself if possible to avoid hacking it with PHP.