Mongo组聚合函数始终返回null

时间:2019-07-14 01:28:28

标签: mongodb mongoose graphql aggregate aggregate-functions

我有以下模型代码的mongodb模型,

const PersonModel = Mongoose.model("person", {
    firstname: String,
    lastname: String,
    timestamp:{ type : Date, default: Date.now },
});

然后也是如下的graphql查询

const PersonType = new GraphQLObjectType({
    name: "Person",
    fields: {
        id: { type: GraphQLID },
        firstname: { type: GraphQLString },
        lastname: { type: GraphQLString },
        timestamp:{ type: GraphQLString },



    }
});

const schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: "Query",
        fields: {
            people: {
                type:GraphQLList(PersonType),
                resolve: (root, args, context, info) => {
                    return PersonModel.aggregate([
                        {"$group" : {_id:"$firstname"}}
                    ]).sort({timestamp:-1}).limit(1)}
                }
            }});

但是,当我通过如下所示的apollo服务器查询名字

query {
  people {
    firstname
  }
}

名字总是返回Null

{
  "data": {
    "people": [
      {
        "firstname": null
      },
      {
        "firstname": null
      },
      {
        "firstname": null
      }
    ]
  }
}

我的总量中有东西吗?

我需要做的是将所有名字与最大时间戳分组

0 个答案:

没有答案
相关问题