从文档中获取子文档 [英] getting subdocument from document
问题描述
我有一个包含子文档的集合,我正在尝试找到一种简单的方法来获取准确的文档.
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"
}
]
}
我想做的是根据其 guid 获取某个部门,但到目前为止,我没有找到任何干净的东西来获取我想要的文档.
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.
我想得到这样的结果:
{
"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"
}
我一直在查看 mongodb 文档,但到目前为止没有任何效果,我得到的最接近的是使用 array_search,但如果可能的话,我想直接从 mongo 本身获取它,以避免用 PHP 对其进行黑客攻击.
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.
推荐答案
也许是这样的:db.collection.find( { "Departments.Guid": "some_value" } )
https://docs.mongodb.com/manual/tutorial/query-embedded-文档/
这篇关于从文档中获取子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!