1

I need to convert mongodb query into java. I have "conversations" collection. Mongo db query this way and it's work.

{
    messages: { source: 1, $elemMatch: { date: { $gte: ISODate("2013-07-25 00:00:00"), $lt: ISODate("2013-08-26 00:00:00")}}}
}

And I am tring into java BasicDBObject

elemMatch = new BasicDBObject();
BasicDBObject retrievedField = new BasicDBObject();
elemMatch.put("date", new    BasicDBObject("$gte",StartDate).append("$lte",EndDate));
elemMatch.put("source", 1);
BasicDBObject up = new BasicDBObject();
up.put("$elemMatch",elemMatch);
retrievedField.put("messages", up);
DBCursor cursor  = this.dbCollection.find( retrievedField).limit(10);

but this code not working

My collection's data is

    {
    "_id" : ObjectId("51f130d6e4b0bf50e9bfc038"),
    "saleId" : 2.43564e+07,
    "saleCode" : "905155440001",
    "randomId" : 9630,
    "creatorId" : 8.21048e+06,
    "recipientId" : 4.83831e+06,
    "creatorType" : 1,
    "returnReasonId" : 11,
    "conversationClosed" : false,
    "operatorCalled" : false,
    "sellerNotified" : false,
    "buyerNotified" : false,
    "operatorCalledDate" : null,
    "creationDate" : ISODate("2013-07-25T14:06:14.967Z"),
    "lastUpdateDate" : ISODate("2013-08-15T08:46:10.115Z"),
    "messages" : [ 
        {
            "senderId" : 8.21048e+06,
            "source" : 1,
            "seenByBuyer" : true,
            "seenBySeller" : true,
            "seenByOperator" : true,
            "date" : ISODate("2013-07-25T14:06:39.968Z"),
            "messageBody" : "asdad"
        }, 
        {
            "senderId" : 8.21048e+06,
            "source" : 1,
            "seenByBuyer" : true,
            "seenBySeller" : true,
            "seenByOperator" : true,
            "date" : ISODate("2013-07-25T14:06:59.978Z"),
            "messageBody" : "asdasdawdwa"
        }, 
        {
            "senderId" : 0,
            "source" : 4,
            "seenByBuyer" : true,
            "seenBySeller" : true,
            "seenByOperator" : true,
            "date" : ISODate("2013-07-25T14:07:20.044Z"),
            "messageBody" : "ad"
        }
    ]
}

Any help?

Calendar calStartDate = Calendar.getInstance();
        Calendar calEndDate = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        Date StartDate = simpleDateFormat.parse(messageStartDate);
        Date EndDate = simpleDateFormat.parse(messageEndDate);

        calStartDate.setTime(StartDate);
        calEndDate.setTime(EndDate);
        Date sdate = new DateTime(calStartDate.get(Calendar.YEAR),calStartDate.get(Calendar.MONTH), calStartDate.get(Calendar.DAY_OF_MONTH),calStartDate.get(Calendar.HOUR),calStartDate.get(Calendar.MINUTE), DateTimeZone.UTC).toDate();
        Date edate = new DateTime(calEndDate.get(Calendar.YEAR), calEndDate.get(Calendar.MONTH), calEndDate.get(Calendar.DAY_OF_MONTH), calEndDate.get(Calendar.HOUR), calEndDate.get(Calendar.MINUTE), DateTimeZone.UTC).toDate();

        BasicDBObject query = new BasicDBObject(
                "messages",
                new BasicDBObject("$elemMatch",new BasicDBObject("source", 1).append("date", new BasicDBObject("$gte", sdate).append("$lt", edate)))
        );

        DBCursor cursor  = this.dbCollection.find( query).limit(10);
0

1 Answer 1

1

Your shell query was not correct anyway. It should be:

{
    "messages": { 
       "$elemMatch": { 
          "source": 1,
          "date": { 
              "$gte": ISODate("2013-07-25 00:00:00"),
              "$lt": ISODate("2013-07-26 00:00:00")
         }
       }
   }
}

And it helps to follow the same nesting rules so you don't get the flow incorrect:

BasicDBObject query = new BasicDBObject(
    "messages",
    new BasicDBObject(
        "$elemMatch",
        new BasicDBObject( "source", 1 )
            .append( new BasicDBObject(
               "date",
               new BasicDBObject( "$gte", StartDate )
                   .append( "$lt", EndDate )
            )
        )
    )
); 

Make sure that "dates" are of type java.util.Date returned from something like jodatime to be valid for serialization as well. And make sure that are UTC times you are constructing from.

Example:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

Date StartDate = new DateTime(2013, 7, 25, 0, 0, DateTimeZone.UTC).toDate();
Sign up to request clarification or add additional context in comments.

3 Comments

SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); Date StartDate = simpleDateFormat.parse(messageStartDate); StartDate and endDate is this way is it?
@neoerol No those are strings. You need to pass in a date object like I said. MongoDB stores as a "Date" BSON type and not a string. Use something that produces java.util.Date as I said in the answer.
@neoerol Added a simple example to follow at the end of the answer.

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.