MongoDB sorting by date as type String

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



MongoDB sorting by date as type String



Can someone help me with the query for sorting an array by date in ascending order?
I have tried the below query but the sorting is not happening as expected,


db.getCollection(xyz).aggregate([

$match:
"_id":$in:["a" : "NA","b" : "HXYZ","c" : "12345","d" : "AA"]

,
$sort: 'bal.date': 1
,

$project:
balances: $slice: ["$bal",2]





])



My collection:


/* 1 */

"_id" :
"a" : "NA",
"b" : "HXYZ",
"c" : "12345",
"d" : "AA"
,


"bal" : [

"type" : "E",
"date" : "2015-08-02"

,


"type" : "E",
"date" : "2015-08-01"


,

"type" : "E",
"date" : "2015-07-07"




]



Please help me what is the problem in the above query.
Thanks in advance.





Please post your sample collection...
– Anthony Winzlet
Aug 8 at 7:54





@AnthonyWinzlet I have added the sample data.
– Shilpa
Aug 8 at 10:06





Even with the string format it is working for me... Check it mongoplayground.net/p/qpf7kM8EUCL... I think your query syntax is incorrect here
– Anthony Winzlet
Aug 8 at 10:12






@AnthonyWinzlet Can you post the query which your using here.Am unable to access the link provided above
– Shilpa
Aug 8 at 10:29






Possible dupe of stackoverflow.com/questions/13449874/…
– Veeram
Aug 8 at 12:26




2 Answers
2



You are mixing $match with $sort stage


$match


$sort



Correct syntax to used aggregation pipeline stages


db.collection.aggregate([
"$match":
"_id":
"$eq":
"a": "NA",
"b": "HXYZ",
"c": "12345",
"d": "AA"


,
"$unwind": "$bal" ,
"$sort": "bal.date": 1 ,
"$group":
"_id": "$_id",
"bal":
"$push": "$bal"


])





Its not working @Anthony Winzlet
– Shilpa
Aug 8 at 10:48





mongoplayground.net/p/-57GwmdpJTX ??
– Anthony Winzlet
Aug 8 at 10:49





I have modified the collection....can you help now ??
– Shilpa
Aug 8 at 12:03






Updated the answer have a look
– Anthony Winzlet
Aug 9 at 2:55





Thanks for the help it worked :) @Anthony Winzlet
– Shilpa
Aug 9 at 4:16



From the looks of it, you're saving the date as String, the sort() function will sort the dates as Strings which will not give you the order you're expecting.



You can either run a script that will convert the bal.date field to Date() format and then sort() will work automatically, or you can do the converting + sorting server side.





the collection I fixed and the date is stored as string only in collection and I cant perform convertion and sorting at server side so can yu help me with alternative way
– Shilpa
Aug 8 at 10:32





The date must be stored as Date() if you want to sort it automatically using MongoDB sort() function. First have a backup to your database and have an update on your collection that changes the bal.date into new Date(bal.date)
– Hamza Wahabi
Aug 8 at 10:34






I will just hit the collection and the collection is maintained by other team.So no way of upgrading as well :(
– Shilpa
Aug 8 at 10:40






You can get a copy or a mock in as your local database, and test the update the collection. And provide the script for the other team and ask them to backup the database and update he collection.
– Hamza Wahabi
Aug 8 at 10:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard