Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2422

Example to convert date to string using $dateToString in Mongodb Java driver

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Done
    • Icon: Minor - P4 Minor - P4
    • None
    • 3.4.1
    • API
    • None
    • Mongodb 3.4, java driver 3.4.1

    Description

      HI,

      Would you be able to provide an example to convert ISODate field to string using the MOngoDb Java Driver. The sample mongoshell query is as follows.

      db.Merchant.aggregate([
                         {$project: {
                                     yearMonthDay: { $dateToString: { format: "%Y-%m-%d", date: "$createddate" } }
                                     ,time:        { $dateToString: { format: "%H:%M:%S:%L", date: "$createddate" } }
                                   }},
      				   { $sort: {createddate : -1}}
                         ])
      

      It will be really helpfull. I tried different combination (as given below) but couldnt figure out how to achieve the above query coded using the java driver.

      public ArrayList<Document> testAggregation5d() {
       
              SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
              
              Object format = new Object() {
                  public String format = "%Y-%m-%d";
                  public String date = "$promotion_list.pro_start_date";
              };
       
              Object a[] = new Object[]{format};
         
         
              
              MongoCollection<Document> collection = databasePuttPutt.getCollection("Merchant");
              return collection.aggregate(
                      Arrays.asList(
                              Aggregates.unwind("$promotion_list"),
                              Aggregates.addFields(new Field("promotion_list.merchantId", "$ownerid")),
                              /*
                              Aggregates.project(
                                      Projections.fields(
                                              Projections.include("promotion_list.merchantId", "promotion_list.pro_id", "promotion_list.pro_type", "promotion_list.pro_description", "promotion_list.pro_code", "promotion_list.pro_start_date", "promotion_list.pro_end_date")
                                             ,Projections.computed("promotion_list.pro_end_ymd",  new Document("$dateToString", new SimpleDateFormat("dd/mm/yyyy").parse("promotion_list.pro_start_date"))))
                                      )
                              ),
                               */
                              //Aggregates.project(Document.parse("{ITimes10: {$multiply: ['$ownerid', 10]}}")),
                              //Aggregates.project(Projections.computed("promotion_list.pro_end_ymd",new Document("$dateToString", Arrays.asList(a)))),
                              Aggregates.project(Projections.computed("promotion_list.pro_end_ymd",new Document("$dateToString", formatDate.format("$promotion_list.pro_start_date")))),
                              Aggregates.replaceRoot("$promotion_list"),
                              Aggregates.sort(orderBy(ascending("pro_created_date"))),
                              Aggregates.limit(4)
                      )
              ).into(new ArrayList<Document>());
       
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            himnaz@gmail.com himnaz
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: