user3062459
user3062459

Reputation: 1631

Mongo query - Return only inner child Key Value

I want to return only the value for DOC.AMOUNT greater than 9 Million. The problem is I am getting the parent DOC, where as I just want the child AMOUNT: 9000000. In other words, instead of {Key: {Key:Value}} I want {Key:Value}}.

My query:

db.acris.find({"DOC. AMOUNT" : {$gt : 9000000}},{"DOC. AMOUNT":1,_id:0}).pretty().limit(5)

Here is the result of my query:

{ "DOC" : { " AMOUNT" : 9000100 } }
{ "DOC" : { " AMOUNT" : 9001000 } }
{ "DOC" : { " AMOUNT" : 9002135.18 } }
{ "DOC" : { " AMOUNT" : 9002292 } }
{ "DOC" : { " AMOUNT" : 9003572 } }

How can I change the query so that I only get {" AMOUNT" : value } ?

Upvotes: 0

Views: 785

Answers (1)

chridam
chridam

Reputation: 103365

You can use the aggregation framework's $project operator to reshape your documents. This will be piped after the $match pipeline which has the query logic and the $limit step which will pass the first 5 documents to be projected:

db.acris.aggregate([
    // acts as your query filter, similar to find()
    { "$match": { "DOC. AMOUNT": { "$gt": 9000000 } } }, 

    // similar to limit(5)
    { "$limit": 5 }, 

    // reshape your documents here
    {
        "$project": {
            "_id": 0,
            "AMOUNT": "$DOC. AMOUNT"
        }
    }
])

Upvotes: 3

Related Questions