Pregunta

I am a Mongo DB beginner.

I have to create a collection that can store events. All the documents in the Event will have a FROM_TIME and TO_TIME keys whose values can vary as follows :

  1. {FROM_TIME : "September 1,1939", TO_TIME : "August 1945"}
  2. {FROM_TIME : "August 15,1947", TO_TIME : "August 15,1947"}
  3. {FROM_TIME : "1857", TO_TIME : "1858"}
  4. {FROM_TIME : "December 7,1944 11:00", TO_TIME : "December 7,1944 14:40"}
  5. {FROM_TIME : "January 1971", TO_TIME : "November 1972"}

Given the random occurrence of the FROM_TIME and TO_TIME, will it be possible to query for events like :

  1. List of events in the second half of the 19th century
  2. List of events on August 15 and so on.

I request people here to provide leads in case I need to rethink the structure of my collection as I'm quite naive at Mongo(or any other non-relational)DB.

***1st Edit begins here

I wrote the following code:

DBObject dbObject = new BasicDBObject();

        dbObject.put("EVNTATMC_ID", new ObjectId());
        dbObject.put("EVNTATMC_DESC", "Pearl Harbour");
        dbObject.put("EVNTATMC_FROM_TIME", Date.valueOf("1944-12-07"));
        dbObject.put("EVNTATMC_TO_TIME", Date.valueOf("1944-12-07"));
        dbObject.put("EVNTATMC_EVNTCMPD_ID", new ObjectId());
        dbObject.put("EVNTATMC_MTRL_ID", null);
        // dbObject.put("EVNTATMC_AUDIT", 1);
        dbObject.put("EVNTATMC_REMARKS", "First insert");

        eventAtomic.insert(dbObject);
dbObject = new BasicDBObject();

        dbObject.put("EVNTATMC_ID", new ObjectId());
        dbObject.put("EVNTATMC_DESC", "1857 Revolt");
        dbObject.put("EVNTATMC_FROM_TIME", "1857");
        dbObject.put("EVNTATMC_TO_TIME", "1858");
        dbObject.put("EVNTATMC_EVNTCMPD_ID", new ObjectId());
        dbObject.put("EVNTATMC_MTRL_ID", null);
        // dbObject.put("EVNTATMC_AUDIT", 1);
        dbObject.put("EVNTATMC_REMARKS", "Only years");

        eventAtomic.insert(dbObject);

Here, I dumbly used String to store incomplete dates but how should I store just an year like 1992, 2001, 2012 or just a month like January, September or something like August 2010 ? I'm not sure as to whether I'm expecting the correct thing ! Please guide me as to how I must handle such 'incomplete' timings.

***1st Edit ends here

¿Fue útil?

Solución

Store the FROM_TIME and TO_TIME as DateTime. See Here: http://www.mongodb.org/display/DOCS/Dates

Then, you can query (rang query for example)
see the answer here: https://stackoverflow.com/a/8136834/131809

So, for all events on August 15th 2012:

var start = new Date(2012, 08, 15);
var end = new Date(2012, 08, 16);

db.myEventCollection.find({created_on: {$gte: start, $lt: end}});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top