
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Implement $dateToString on array items with MongoDB
To implement $dateToString on array items, use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo104.insertOne( ... { ... ... "AppName" : "Online Book", ... "Details" : [ ... { ... "ClientName" : "Chris", ... "Deadline" : new ISODate("2020-03-10") ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2ed7fd9fd5fd66da21446f") }
Display all documents from a collection with the help of find() method −
> db.demo104.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e2ed7fd9fd5fd66da21446f"), "AppName" : "Online Book", "Details" : [ { "ClientName" : "Chris", "Deadline" : ISODate("2020-03-10T00:00:00Z") } ] }
Following is the query to implement $dateToString on array items −
> db.demo104.aggregate([ ... { "$match": {}}, ... { "$project": { ... "title": 1, ... "Details": { ... "$map": { ... "input": "$Details", ... "as": "out", ... "in": { ... "ClientName": "$$out.ClientName", ... "Deadline": { ... "$dateToString": { "format": "%m", "date": "$$out.Deadline" } ... } ... } ... } ... } ... }} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e2ed7fd9fd5fd66da21446f"), "Details" : [ { "ClientName" : "Chris", "Deadline" : "03" } ] }
- Related Articles
- How to count items in array with MongoDB?
- Implement array match in MongoDB?
- MongoDB find by multiple array items?
- Clearing items in a nested MongoDB array?
- Get array items inside a MongoDB document?
- Implement MongoDB $push in embedded document array?
- MongoDB Query to implement $in in array
- MongoDB find by multiple array items using $in?
- MongoDB projection result as an array of selected items?
- Multi-key Indexing on an entire array with MongoDB?
- Filter query on array of embedded document with MongoDB?
- JavaScript - find distance between items on array
- Sort id and reverse the items with MongoDB
- Grouping the array items in MongoDB and get the count the products with similar price?
- Count the number of items in an array in MongoDB?

Advertisements