Sorting MongoDB data in GraphQL resolver (Meteor/React/Apollo)

Ajay Pillay

I'm trying to sort some data returned by a query in GraphQl. I'm running a Meteor/React/Apollo/Graphql stack, and this line allows me to obtain all the data in my database in a resolver resolvers.js : return database.find({}) I would like to sort it server-side by the "name" field and according to the docs and everything else I've been able to find online so far, return database.find({}).sort({name:1}) should have cut it, but it's not actually returning anything and I can't seem to figure out why, nothing shows up in my console and no errors are being thrown, and hence I believe it's just null or empty.

In my Robo3T console I can run database.find({}).sort({name:1}) and I can see the data actually being sorted. According to the docs I thought it may have been due to the Node implementation, and so I tried sort([["name",1]]) but that did not work either, and I'm not quite sure how else to go about this.

If there is no way to do this - should I just rely on client-side sorting? It's not many entries, < 100, and I think I should just sort it in the database itself because I never need the unsorted data and it's likely not even going to change much.

I would like to ask this regardless though, even if just sorting it once in my database is enough for this specific situation, how would I otherwise go about sorting data in a resolver, because I would need to do this in other instances?

Thanks!

편집-이것이 내가 가진 것입니다.

export default {
    Query: {
        test_query(obj, args, context) {
            // Bottom line is output
            console.log("test1");
            console.log(Database.find({}).sort({name:1}));
            // Bottom line is not output
            console.log("test2");
            return Database.find({});
        },
}

EDIT2 : 내 데이터베이스는 다음과 같이 정의됩니다. Database = new Mongo.Collection("db_name")

크리스티안 프리츠

나는 당신이 잘못된 문서를보고 있다고 생각합니다. 이것이 실제로 유성이라면 원시 몽고 컬렉션이 아니라 Database아마도 유성 컬렉션입니다. 유성 컬렉션의 인터페이스는 약간 다릅니다. 귀하의 경우에는 다음을 원한다고 생각합니다.

Database.find({}, {sort: {name: 1}}).fetch();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Mongodb Aggregation Framework Sorting the huge data

분류에서Dev

Sorting number values stored as a BigDecimal type with Spring Data MongoDB in MongoDB

분류에서Dev

Excel Sorting "mixed data"

분류에서Dev

Apollo Graphql modify input data

분류에서Dev

Apollo GraphQL Deconstruction of data not working

분류에서Dev

fetching data from graphql to react

분류에서Dev

EmberJS - Sorting data prior to showing

분류에서Dev

Graphql Query with express and Mongodb won't work properly?

분류에서Dev

Topological sorting of C++ data structures

분류에서Dev

Large excel data sorting VBA help or function?

분류에서Dev

Sorting and limiting MongoDB queries by insertion order in a multi-process environment

분류에서Dev

mongoDB with nodejs return data

분류에서Dev

ID가 아닌 이름으로 항목을 가져 오는 GraphQL Resolver

분류에서Dev

type-graphql : Resolver에서 반환되는 필드를 확인하는 방법

분류에서Dev

Sorting MySQL SELECT data from multiple tables with unique data

분류에서Dev

How to Update data in MongoDb with Java?

분류에서Dev

get mongodb data to nodejs array?

분류에서Dev

How to insert data into MongoDB with Laravel

분류에서Dev

Retrieve MongoDB data and store it in a list

분류에서Dev

Invalid data in mongoDB after insert of an ISO data

분류에서Dev

Sorting data from a large text file and convert them into an array

분류에서Dev

Angularjs ng-table with AJAX data source, sorting and filtering

분류에서Dev

MySql sorting hierarchical data in a closure table that has repeated nodes

분류에서Dev

Sorting array of objects based on data in the object, and nesting them

분류에서Dev

C# inline sorting ObservableCollection does not update Data Binding

분류에서Dev

Validate and format JSON without alphabetically sorting the data with 'jsonlint'

분류에서Dev

Type-GraphQL, TypeORM 및 종속성 주입을 사용하여 Resolver-Inheritance를 구현하는 방법

분류에서Dev

GraphQL 리졸버 (Meteor / React / Apollo)에서 MongoDB 데이터 정렬

분류에서Dev

SpringData MongoDB Sorting이 예상대로 작동하지 않습니다.

Related 관련 기사

  1. 1

    Mongodb Aggregation Framework Sorting the huge data

  2. 2

    Sorting number values stored as a BigDecimal type with Spring Data MongoDB in MongoDB

  3. 3

    Excel Sorting "mixed data"

  4. 4

    Apollo Graphql modify input data

  5. 5

    Apollo GraphQL Deconstruction of data not working

  6. 6

    fetching data from graphql to react

  7. 7

    EmberJS - Sorting data prior to showing

  8. 8

    Graphql Query with express and Mongodb won't work properly?

  9. 9

    Topological sorting of C++ data structures

  10. 10

    Large excel data sorting VBA help or function?

  11. 11

    Sorting and limiting MongoDB queries by insertion order in a multi-process environment

  12. 12

    mongoDB with nodejs return data

  13. 13

    ID가 아닌 이름으로 항목을 가져 오는 GraphQL Resolver

  14. 14

    type-graphql : Resolver에서 반환되는 필드를 확인하는 방법

  15. 15

    Sorting MySQL SELECT data from multiple tables with unique data

  16. 16

    How to Update data in MongoDb with Java?

  17. 17

    get mongodb data to nodejs array?

  18. 18

    How to insert data into MongoDB with Laravel

  19. 19

    Retrieve MongoDB data and store it in a list

  20. 20

    Invalid data in mongoDB after insert of an ISO data

  21. 21

    Sorting data from a large text file and convert them into an array

  22. 22

    Angularjs ng-table with AJAX data source, sorting and filtering

  23. 23

    MySql sorting hierarchical data in a closure table that has repeated nodes

  24. 24

    Sorting array of objects based on data in the object, and nesting them

  25. 25

    C# inline sorting ObservableCollection does not update Data Binding

  26. 26

    Validate and format JSON without alphabetically sorting the data with 'jsonlint'

  27. 27

    Type-GraphQL, TypeORM 및 종속성 주입을 사용하여 Resolver-Inheritance를 구현하는 방법

  28. 28

    GraphQL 리졸버 (Meteor / React / Apollo)에서 MongoDB 데이터 정렬

  29. 29

    SpringData MongoDB Sorting이 예상대로 작동하지 않습니다.

뜨겁다태그

보관