2013-04-22 64 views
0

在我的文档中,如果一个键的值本身是一个JSON(具有键值)。 我该如何搜索值内的文档的关键。我们可以查询值内的键

为前:

{ "_id": { "$oid" : "51711cd87023380037000001" }, 
    "dayData": "{ "daysdata":{"date":"02-12- 2013","week_day":"","month":"","date_day":"","year":"2013"}}" 
    } 

如果我有多个文件,像这样的,我想要得到的有单据日期“02-12- 2013”​​。我的返回值应该使用MongoDB的Java驱动程序是关键daydata的价值

+0

[在mongo上查询内部函数]的可能重复(http://stackoverflow.com/questions/16140978/querying-on-internals-in-mongo) – mnemosyn 2013-04-22 10:06:23

回答

2

您可以使用点符号,所以你的情况:

db.collection.ensureIndex({'dayData.daysdata.date': 1}); 

然后

db.collection.find({'dayData.daysdata.date': 02-12-2013}); 

为例:

> db.test.insert({x: {x: {x: 1}}}) 
> db.test.insert({x: {x: {x: 2}}}) 
> db.test.insert({x: {x: {x: 3}}}) 
> 
> db.test.ensureIndex({'x.x.x': 1}) 
> 
> db.test.find({'x.x.x': 2}, {_id: false}) 
    {"x" : { "x" : { "x" : 2} } } 
> 

证明其使用该索引:

> db.test.find({'x.x.x': 2}, {_id: false}).explain() 
    { 
    "cursor" : "BtreeCursor x.x.x_1", 
    "n" : 1, 
    "nscannedObjects" : 1, 
    "nscanned" : 1, 
     .... 
    } 
> 
相关问题