I'm new to MongoDB. I'm using laravel-mongodb.
I have a document in tree structure. I need to get documents for a specific year. Here is the image of how my data is:
I need to select all documents that have the period 2017. my problem is that the period is a field of type object. And the value is the index. Does anyone know how I can do this?
You can try below query :
db.collection.aggregate([
/** As we need to filter on key but not on key's value,
* So convert periodos object to an array which makes {k : 2017, v : value of 2017} etc.. */
{
$addFields: {
periodos: {
$objectToArray: "$periodos"
}
}
},
/** Filter docs to check periodos has 2017 */
{
$match: {
"periodos.k": "2017"
}
},
/** convert periodos array back to object to its original type */
{
$addFields: {
periodos: {
$arrayToObject: "$periodos"
}
}
}
])
Test : MongoDB-Playground
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다