mongodb查询嵌入式文档密钥作为日期 [英] mongodb query embedded doc key as date

查看:84
本文介绍了mongodb查询嵌入式文档密钥作为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的带嵌入式文档的mongodb文档. 事件"是文档列表(BasicDBList),其中每个文档都以键作为某个日期(例如2013年1月1日)存储,值是一堆字段.通过这种方式定义了结构,这样我就可以获取在某个日期(例如2013年1月1日)发生的所有事件.

我有两个问题:

  1. 是否有更好的方法来构造此文档?我不确定钥匙 约会是个好主意,但同时我想 轻松检索所有文档,并根据日期将它们存储在内存中.什么时候 我检索文档,我想要一个键为日期和值的哈希表 作为该日期的文档列表(使用Java).

  2. 如何通过传递日期来检索文档?例如,我想要 键为2013年1月1日的所有文档.查询内容是什么 在Java中?

谢谢

解决方案

通常,不允许使用json键进行查询,但是可以使用以下技巧:

db.things.find( { key : { $exists : true } } );

此查询用于查找具有特定键的文档

但是,我认为这不是解决问题的最佳方法.首先,文档的事件对象似乎在将来会不可预测地增长,这是我们在设计架构时应避免的事情,因为随着文档的增长,mongodb必须重新定位磁盘空间来存储它,这将导致性能问题.

我建议将事件对象存储在单独的集合中,所有者"和日期"字段是您要查询的对象.

{ 
  "owner":"xyz@yahoo.com",
  "date":"Jan 1, 2013",
  "events":[  
    {  
       "desc": "My Desc",  
       "title": "My Title",  
       "createDateTime": "May 7, 2013 8:08:13 AM",  
       "updateDateTime": "May 7, 2013 8:08:13 AM"  
    }
  ]
}

Here is my mongodb document with embedded documents. The "events" is a list of documents (BasicDBList) in which each document is stored with key as some date (e.g. Jan 1, 2013) and value is a bunch of fields. The structure is defined this way so I can get all events happened on some date (e.g. Jan 1, 2013).

I have two questions:

  1. Is there a better way to structure this document? I am not sure key as a date is a good idea but at the same time I want to retrieve all documents easily and store them in memory based on date. When I retrieve documents, I want a Hashtable with key as date and value as a List of documents for that date (using Java).

  2. How do I retrieve documents by passing a date? For example, I want all the documents with key as Jan 1, 2013. What would be the query in Java?

{  
  "_id": {  
    "_time": 1367928493,  
    "_machine": -1914548796,  
    "_inc": -1784811303,  
    "_new": false  
  },  
  "email": "xyz@yahoo.com",  
  "events": {  
    "Jan 1, 2013": [  
      {  
        "desc": "My Desc",  
        "title": "My Title",  
        "createDateTime": "May 7, 2013 8:08:13 AM",  
        "updateDateTime": "May 7, 2013 8:08:13 AM"  
      },  
      {  
        "desc": "My Desc2",  
        "title": "My Title2",  
        "createDateTime": "May 7, 2013 8:08:13 AM",
        "updateDateTime": "May 7, 2013 8:08:13 AM"
      }
    ],
    "Feb 1, 2013": [
      {
        "desc": "My Desc3",
        "title": "My Title3",
        "createDateTime": "May 8, 2013 8:08:13 AM",
        "updateDateTime": "May 7, 2013 8:08:13 AM"
      },
      {
        "desc": "My Desc3",
        "title": "My Title3",
        "createDateTime": "May 8, 2013 8:08:13 AM",
        "updateDateTime": "May 8, 2013 8:08:13 AM"
      }
    ]
  }
}

Thank you

解决方案

Typically, you are not allowed to query by a json key, but you can use this trick:

db.things.find( { key : { $exists : true } } );

this query is to find the document which has a specific key

However, I believe this is not the best approach to solve your prolem. First, the events object of the document seems will grow unpredictably in the future, this is what we should avoid in designing the schema, because as the document grows, mongodb has to relocate the disk space to store it, which will cause performance problem.

I recommend to store the events object in a seperate collection, the 'owner' and 'date' fields are what you will query by.

{ 
  "owner":"xyz@yahoo.com",
  "date":"Jan 1, 2013",
  "events":[  
    {  
       "desc": "My Desc",  
       "title": "My Title",  
       "createDateTime": "May 7, 2013 8:08:13 AM",  
       "updateDateTime": "May 7, 2013 8:08:13 AM"  
    }
  ]
}

这篇关于mongodb查询嵌入式文档密钥作为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆