[英]Sorting MongoDB data in GraphQL resolver (Meteor/React/Apollo)
I'm trying to sort some data returned by a query in GraphQl.我正在尝试对 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.我正在运行 Meteor/React/Apollo/Graphql 堆栈,这一行允许我在解析器
resolvers.js
中获取数据库中的所有数据: return database.find({})
我想对它进行排序服务器-在“名称”字段旁边,根据文档以及到目前为止我能够在网上找到的所有其他内容, return database.find({}).sort({name:1})
应该已经删除了它,但它是实际上没有返回任何东西,我似乎无法弄清楚为什么,我的控制台中没有显示任何内容,也没有抛出任何错误,因此我相信它只是 null 或空的。
In my Robo3T console I can run database.find({}).sort({name:1})
and I can see the data actually being sorted.在我的 Robo3T 控制台中,我可以运行
database.find({}).sort({name:1})
并且可以看到实际正在排序的数据。 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.根据 文档,我认为这可能是由于节点实现,所以我尝试了
sort([["name",1]])
但这也不起作用,我不太确定 go 的其他方法对这个。
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.
它的条目并不多,< 100,我认为我应该在数据库本身中对其进行排序,因为我从不需要未排序的数据,而且它甚至可能不会发生太大变化。
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!谢谢!
EDIT - This is what I have:编辑 - 这就是我所拥有的:
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: My database is defined as Database = new Mongo.Collection("db_name")
EDIT2:我的数据库定义为
Database = new Mongo.Collection("db_name")
I believe you are looking at the wrong documentation.我相信您正在查看错误的文档。 If this is indeed meteor, then
Database
is presumably a meteor collection , not a raw mongo collection.如果这确实是 meteor,那么
Database
大概是meteor 集合,而不是原始 mongo 集合。 The interface on meteor collections is slightly different. meteor collections上的接口略有不同。 In your case, I think you want:
就你而言,我认为你想要:
Database.find({}, {sort: {name: 1}}).fetch();
.find()
returns a cursor: .find()
返回 cursor:
http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#find http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#find
This works for Meteor pubsub, but not for GraphQL/Apollo.这适用于 Meteor pubsub,但不适用于 GraphQL/Apollo。 Try
.find().toArray()
:尝试
.find().toArray()
:
http://mongodb.github.io/node-mongodb-native/3.6/api/Cursor.html#toArray http://mongodb.github.io/node-mongodb-native/3.6/api/Cursor.html#toArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.