Code SnippetGolangMongo-mgo

Golang mgo sort number inside a string

// Get List
func (z *DocumentTagService) GetDocumentTags() ([]models.DocumentTag, error) {
	z.DatabaseService.Connect()
	var o []models.DocumentTag
	err := (*z.DatabaseService.Db).C(z.Collection).Find(bson.M{}).Sort("-$natural", "-documentReference").All(&o)
	return o, err
}

Result

[
    {
        "idDocumentTag": "5b1390f29ebea41854dcc0dc",
        "documenReference": "MCTU/2018/16",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390f09ebea41854dcc0db",
        "documenReference": "MCTU/2018/15",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390d19ebea41854dcc0da",
        "documenReference": "MCTU/2018/14",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390d09ebea41854dcc0d9",
        "documenReference": "MCTU/2018/13",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390cf9ebea41854dcc0d8",
        "documenReference": "MCTU/2018/12",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390cd9ebea41854dcc0d7",
        "documenReference": "MCTU/2018/11",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390b29ebea41854dcc0d6",
        "documenReference": "MCTU/2018/10",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390b19ebea41854dcc0d5",
        "documenReference": "MCTU/2018/9",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390b09ebea41854dcc0d4",
        "documenReference": "MCTU/2018/8",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b1390ae9ebea41854dcc0d3",
        "documenReference": "MCTU/2018/7",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b13900c9ebea41854dcc0d2",
        "documenReference": "MCTU/2018/6",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b13900c9ebea41854dcc0d1",
        "documenReference": "MCTU/2018/5",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b13900b9ebea41854dcc0d0",
        "documenReference": "MCTU/2018/4",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b13900a9ebea41854dcc0cf",
        "documenReference": "MCTU/2018/3",
        "company": "MCTU"
    },
    {
        "idDocumentTag": "5b138b449ebea41854dcc0ce",
        "documenReference": "MCTU/2018/2",
        "company": "MCTU"
    }
]

Leave a Reply

Your email address will not be published. Required fields are marked *