簡體   English   中英

MongoDB查詢的Spring聚合表示形式

[英]Spring Aggregate representation of mongodb query

有人可以告訴我下面查詢的Spring表示嗎?

db.groupsDocument.aggregate(
{$unwind:"$groups"},
{$match:{"groups.students":"8b8a7464-4dff-4136-ab0f-ec2bfe1ec48e"}},
{$group:{ _id:"$_id",groups:{$push:"$groups"},createdDate:{$first:"$createdDate"},editedDate:{$first:"$editedDate"},editedBy:{$first:"$editedBy"}}})

上面的查詢是工作文件。

我嘗試了下面的代碼,但無法正常工作。 它引發錯誤“找不到GroupsDocument類型的屬性_id!”

GroupOperation groupOperation=Aggregation.group("$_id").push("$groups").as("groups").first("$createdDate").as("createdDate")
                        .first("$editedDate").as("editedDate")
                        .first("$editedBy").as("editedBy");

 Aggregation aggregation = Aggregation.newAggregation(
                    Aggregation.unwind("$groups"),
                    Aggregation.match(where("_id.teacherId").is("5").and("groups.students").in("11")),
                    groupOperation);

 AggregationResults<BasicDBObject> groupResults = groupsMongoTemplate.aggregate(aggregation,
                    GroupsDocument.class, BasicDBObject.class);

我在文件中的編號包含兩個欄位,如下所示

"_id" : { "teacherId" : "1", "Year" : "2016" }

文件結構

{
    "_id" : { "teacherId" : "1", "Year" : "2016" },
    "groups" : [ {
        "groupId" : "123",
        "groupName" : "Test1",
        "groupNameLowerCase" : "test1",
        "description" : "sample document",
        "students" : ["11", "22"]
         },
    {
        "groupId" : "234",
        "groupName" : "Test2",
        "groupNameLowerCase" : "test2",
        "description" : "sample document",
        "students" : ["11", "22"]
         },
        {
        "groupId" : "345",
        "groupName" : "Test3",
        "groupNameLowerCase" : "test3",
        "description" : "sample document",
        "students" : ["21", "32"]
         }
    ],
    "editedBy" : "sachin",
    "createdDate":"11/11/2016"
    "editedDate":" 11/14/2016"

}

類:

@Document
public class GroupsDocument extends AbstractDocument {

    /**
     * document _id and shard key
     *
     * composite key: Teacherid, Year
     */
    @Id
    @Field(FieldDefinition.DOCUMENT_ID)
    private final GroupsDocumentId GroupsDocumentId;

    @Field(FieldDefinition.GROUPS)
    private final Collection<Group> groups;

    @Field(GroupsDocument.FieldDefinition.CREATED_DATE)
    private final Date createdDate;

    @Field(GroupsDocument.FieldDefinition.EDITED_DATE)
    private Date editedDate;

    @Field(GroupsDocument.FieldDefinition.EDITED_BY)
    private String editedBy;

    /**
     * empty constructor
     */
    @SuppressWarnings("unused")
    public GroupsDocument() {
        this(null, null, Collections.emptyList(), null, null);
    }


    public GroupsDocument(String teacherId, String year, Collection<Group> groups, String editedBy) {
        this(teacherId, year, groups, new Date(), editedBy);
    }

    public GroupsDocument(
            String teacherId, String year, Collection<Group> groups, Date createdDate, String editedBy) {
        this.GroupsDocumentId = new GroupsDocumentId(teacherId, year);
        this.groups = groups;
        this.createdDate = createdDate;
        this.editedDate = this.createdDate;
        this.editedBy = editedBy;
    }

    public GroupsDocumentId getGroupsDocumentId() {
        return GroupsDocumentId;
    }

    public Collection<Group> getGroups() {
        return groups;
    }

    public Date getEditedDate() {
        return editedDate;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setEditedDate(Date editedDate) {
        this.editedDate = editedDate;
    }

    public String getEditedBy() {
        return editedBy;
    }

    public void setEditedBy(String editedBy) {
        this.editedBy = editedBy;
    }

ID類別:

public class GroupsDocumentId extends AbstractDocument {

    @Field(GroupsDocument.FieldDefinition.teacherId)
    private final String teacherId;

    @Field(GroupsDocument.FieldDefinition.year)
    private final String year;

    /**
     * empty constructor
     */
    @SuppressWarnings("unused")
    public GroupsDocumentId() {
        this(null, null);
    }

    public GroupsDocumentId(String teacherId, String year) {
        this.teacherId = teacherId;
        this.year = year;
    }

    public String getteacherId() {
        return teacherId;
    }

    public String getyear() {
        return year;
    }

將私有最終GroupsDocumentId GroupsDocumentId更改為私有最終GroupsDocumentId _id以進行彈簧映射上下文,以將id變量綁定到GroupsDocument類中的GroupDocumentId類型。

或者,您可以使用此聚合變量繞過類型檢查。

 AggregationResults<BasicDBObject> groupResults = mongoTemplate.aggregate(aggregation,
            "groupsDocument, BasicDBObject.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM