[JAVA-2422] Example to convert date to string using $dateToString in Mongodb Java driver Created: 31/Dec/16  Updated: 01/Jun/23  Resolved: 09/Jan/17

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 3.4.1
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: himnaz Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mongodb 3.4, java driver 3.4.1


Case:

 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>());
 
    }



 Comments   
Comment by Ross Lawley [ 09/Jan/17 ]

Answered on the mongodb-user mailing list.

Generated at Thu Feb 08 08:57:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.