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] 삭제
몇 마디 만하겠습니다