Sumeet Masih
Sumeet Masih

Reputation: 607

Mongodb $unwind for mongoid embedded document

following is an example of schema I have or trying to query to

{
    "_id" : ObjectId("5139f9468efdd10e44000024"),
    "name" : "Test Test",
    "account_activities" : [
        {
            "_id" : ObjectId("5139f9468efdd10e44000025"),
            "activity_id" : ObjectId("5139f6118efdd10e4400000e"),
            "allowance" : 100,
            "base_rate" : {
                "cents" : 80,
                "currency_iso" : "USD"
            },
            "excess_rate" : {
                "cents" : 90,
                "currency_iso" : "USD"
            },
            "quota" : 100,
            "taxable" : true,
            "description" : "",
            "sm" : 1,
            "updated_at" : ISODate("2015-07-24T16:18:50.942Z")
        }
    ],
    "account_number" : "D-061-5556",
    "active" : false,
    "balance" : {
        "cents" : 0,
        "currency_iso" : "USD"
    },
}

On running an $unwind I get nothing, is there something I am doing wrong..?

db.accounts.aggregate([{$unwind: "$account_activities"}, {$project: {_id: 0, name: 1, base_rate: 1}}])

I am using mongo shell version 2.6.12

Upvotes: 0

Views: 138

Answers (1)

sidgate
sidgate

Reputation: 15254

base_rate is still under account_activities. So you have to project it as below

db.accounts.aggregate([
    {$unwind: "$account_activities"}, 
    {$project: {
      _id: 0, 
      name: 1, 
      'account_activities.base_rate': 1
    }}
])

Upvotes: 1

Related Questions