如何将mongoshell命令转换为java代码

时间:2016-07-07 08:15:21

标签: java mongodb

import graphql.schema.GraphQLObjectType;
import static graphql.schema.GraphQLObjectType.newObject;

import static graphql.Scalars.*;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;

import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;

public class GraphQLTypes {

    private GraphQLObjectType studentType;
    private GraphQLObjectType classType;

    public GraphQLTypes() {

       createStudentType();
        createClassType();
    }

    void createStudentType() {
        studentType = newObject().name("Student")
                .field(newFieldDefinition().name("name").type(GraphQLString).build())
            .field(newFieldDefinition().name("currentClass").type(classType).build())
            .build();
    }

    void createClassType() {
        classType = newObject().name("Class")
            .field(newFieldDefinition().name("name").type(GraphQLString).build())
            .field(newFieldDefinition().name("students").type(new GraphQLList(studentType)).build())
            .build();
    }

}

如何将上面的mongo shell命令转换为java代码?任何人都可以帮我解决这个问题......

1 个答案:

答案 0 :(得分:0)

最后,经过这么多研究,能够解决我的问题......

解决方案:

  Document docName = new Document().append("role_id", 1);

        ArrayList<String> list = new ArrayList<>();
        list.add("$dateAdded");
        list.add("$sourceDate");

        List<Document> pipeline = Arrays.<Document> asList(
                new Document("$project", new Document("total", new Document("$subtract", list))));

        AggregateIterable<Document> iterable = collection.aggregate(pipeline);

        for (Document document : iterable) {
            Long copyCount =  (Long) document.get("total");
            System.out.println("copycount" + copyCount);
        }