1

I have the following structure in my MongoDB . I have a nested structure of my collection named chains which is shown below . I am trying to access options of a particular date as shown below which is 2015-01-17 in my case .

db.chains.find({ "symbol" : "UBSC" ,"option_exp.expiration_dt" : "2015-01-17"}).pretty()

But the following query above is returning me all the data related to that Symbol .

{
        "_id" : ObjectId("52000a90d293b0e4134e8c35"),
        "symbol" : "UBSC",
        "option_exp" : [
                {
                        "expiration_dt" : "2015-01-17",
                          "options" : [
                                {
                                        "mult" : "10"
                                },
                                {
                                        "mult" : "10"
                                }
                        ]
                    },
                        {
                        "expiration_dt" : "2014-01-18",
                        "options" : [
                                {

                                        "prem_mult" : "10"
                                },
                                {
                                        "prem_mult" : "10"
                                }

           }
                        ]
                }
        ],
}

This is the way i was trying to access through java

BasicDBObject query = new BasicDBObject();
query.append("symbol", "UBSC");
query.append("option_exp.expiration_dt", "2015-01-17");

Could anybody please help me as how to access data of a particular date .

1 Answer 1

3

Use $elemMatch to limit content of option_exp array field that is included in result:

db.chains.find({symbol : "UBSC" ,"option_exp.expiration_dt" : "2015-01-17"}, 
               {option_exp: {$elemMatch: {expiration_dt: "2015-01-17"}}})

This will select documents which have symbol equal to "UBSC" and option_exp array items with expiration_dt equal to "2015-01-17". Then we limit option_exp array content to items which have required expiration date (otherwise whole document will all option_exp items will be returned).

Sign up to request clarification or add additional context in comments.

9 Comments

hey thanks i tried this option you mentioned , but unfortunately i was getting this below error error: { "$err" : "Unsupported projection option: $elemMatch", "code" : 13097 } , i was using mongo1.6 version
I see that elemMatch has got suppourt since version 2.2 , so is there any other alternative to achieve this .
Prior to 2.2 you'd have to do it in your code after the matched document has come over the wire.
@PreethiJain is it possible that several items with same expiration_dt will be in array? If all dates are different, then you can use operator $: {"option_exp.$":1} but it returns only first matching item from array
@PreethiJain sorry, but I don't know other way to do that in mongo 1.6. You can change schema to have only one item per date (e.g. let it contain all options) - thus $ will work, you can update mongo to 2.2 to use $elemMatch, or you can filter in-memory
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.