0

I am using MongoDB and have collection in the below format .

 db.options.find({ "undersymbol" : "UBSC"}).pretty()
{

        "finalsymbol" : "UBSC",
        "option_exp" : [
                {
                        "expiration_dt" : "2015-01-17",
                        "days_to_expire" : 638,
                        "low_strike" : 120,
                        "high_strike" : 190,
                        "no_of_strikes" : 86,
                        "options" : [
                                {
                                        "strikeprice" : "120.0",
                                        "chain_type" : "call",
                                        "symbol" : "UBSC"

                                }
              ]
                     },

          "expiration_dt" : "2015-01-18",
                        "days_to_expire" : 656,
                        "low_strike" : 34,
                        "high_strike" : 455,
                        "no_of_strikes" : 67,
                        "options" : [
                                {
                                        "strikeprice" : "420.0",
                                        "chain_type" : "call",
                                        "symbol" : "UBSC"

                                }
              ]
                     }


                }
        ]
}

Right now I am using the folllowing query to extract the Data from my collection

           BasicDBObject query = new BasicDBObject();
        query.put("finalsymbol", "UBSC");
        OptionsData ocd = (OptionsData) coll.findOne(query);

The query that is being formed is

{ "finalsymbol" : "UBSC"}

Is it possible to that can i include the expiration_dt also along with finalsymbol in the query , so that the performance will be increased ??

4

2 回答 2

1

是的,您只需要添加一个复合索引,并修改您的查询:

db.options.ensureIndex({finalsymbol:1,expiration_dt:1});

如下修改您的 Java 代码:

BasicDBObject query= new BasicDBObject("finalsymbol", "UBSC")
                              .append("expiration_dt":"2015-01-18");
于 2013-07-28T07:31:14.030 回答
1

如果您需要按到期日期搜索,只需在查询调用中添加另一个字段put(不是append顺便说一句吗?)。如果您需要性能,请在您用于查询集合的字段上定义一个索引。

于 2013-07-27T21:11:57.103 回答