Created
March 24, 2017 15:06
-
-
Save TheSirop/8988ea29da145fb5f2ac05550a9e2da9 to your computer and use it in GitHub Desktop.
Объединение одинаковых полей в коллекции MongoDB (ORM mongoose)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Имеется коллекция Prices. | |
| { | |
| "slug": "0u-00000124", | |
| "createdAt": "2017-03-23T14:05:11.934Z", | |
| "price": 384 | |
| }, | |
| { | |
| "slug": "0u-00000125", | |
| "createdAt": "2017-03-23T14:06:11.934Z", | |
| "price": 129 | |
| }, | |
| { | |
| "slug": "0u-00000125", | |
| "createdAt": "2017-03-23T14:07:11.934Z", | |
| "price": 125 | |
| } | |
| // Необходимо сделать выборку по след. условию: при одинаковых slug оставлять только ту запись у которой (createdAt) самая свежая. | |
| // Моя реализация через mongoose (сделана сортировка по createdAt) и необходимо дописать условие объединения: | |
| Price.find({}, {}, { sort: { createdAt: -1 } }); | |
| // В результате на выходе нужно получить: | |
| { | |
| "slug": "0u-00000124", | |
| "createdAt": "2017-03-23T14:05:11.934Z", | |
| "price": 384 | |
| }, | |
| { | |
| "slug": "0u-00000125", | |
| "createdAt": "2017-03-23T14:07:11.934Z", | |
| "price": 125 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment