MongoDB - Query on key name of an object

Renato Souza de Oliveira

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:

enter image description here

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?

whoami - fakeFaceTrueSoul

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Query object within object

분류에서Dev

How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

분류에서Dev

Query in MongoDb for fetching record

분류에서Dev

Mongodb find query optimization

분류에서Dev

Use the value of a key as a key in mongodb aggregation

분류에서Dev

MongoDB object - PYTHON

분류에서Dev

How to pluck by key name matching?

분류에서Dev

How to get key name with AutoHotkey

분류에서Dev

Convert SQL Server Query to MongoDB Query @ Runtime

분류에서Dev

Parse include key in sub query

분류에서Dev

MongoDB spatial query on source and destination

분류에서Dev

MongoDB Aggregate Query Group by Dates

분류에서Dev

mongodb nested search query in java

분류에서Dev

MongoDb query not returning all the fields

분류에서Dev

MongoDb query not returning all the fields

분류에서Dev

MongoDB: find() query to select nothing

분류에서Dev

Check id of foreign key object

분류에서Dev

PHP: access object with key int

분류에서Dev

DataTable object initializer with Primary Key

분류에서Dev

Sorting array of object by specific key

분류에서Dev

How to access a key object in jQuery with "/"

분류에서Dev

TypeError : undefined는 개체가 아닙니다 ( 'row [OBJECT_COLUMN_NAME] = key'평가). 네이티브 반응

분류에서Dev

In Java is there a name for the object the method is called on?

분류에서Dev

Get an object property name as string

분류에서Dev

How to set the object name on json

분류에서Dev

sql dependency Invalid object name '

분류에서Dev

combine two keys to one key in mongodb

분류에서Dev

MongoDB 제한 mapReduce on index key

분류에서Dev

Does the name of the csr and private key file should match the domain name?