java - 如何在Java Mongodb投影操作中给出$ObjectId

标签 java mongodb aggregation-framework

我是 mongo 的新手。下面是我在 mongodb shell 中执行的聚合操作。但是在我的 java ProjectionAggregation 中,我无法提供 $toObjectId。请纠正我遗漏了什么。

db shell 查询

 db.getCollection('UserData').aggregate([

{
 $project : {
     "username" : "$username",
     "beneficiaries" : "$beneficiaries"
        
     }   
 },
 {
    $unwind :   {
        path : "$beneficiaries",
        preserveNullAndEmptyArrays: true
    }
  },
  {
 $project : {  
     "username" : "$username",
     "beneficiaries" : "$beneficiaries",
      ---- dont know how to give $toObjectId in java ProjectionOperation .
     "beneficiaryStudId" : { $toObjectId : "$beneficiaries.studentId" }  
     }
 },
  {
      $lookup:
         {
           from: "StudentProfileData",
           localField: "beneficiaryStudId",
           foreignField: "_id",
           as: "studProfile"
          }
      }
])

Java代码投影操作

        ProjectionOperation projectUserAndBeneficiaries = Aggregation.project()
                  .andExpression("username").as("username")
                  .andExpression("beneficiaries").as("beneficiaries");

        
        ProjectionOperation projectUserAndOtherDetails = Aggregation.project()
                  .andExpression("username").as("username")
                  .andExpression("beneficiaries").as("beneficiaries")
---- How to give $toObjectId in projection operation             .andExpression("beneficiaries.studentId").as("beneficiaryStudId");
                  
        LookupOperation lookupOperation = LookupOperation.newLookup().
                from("StudentProfileData").
                localField("beneficiaryStudId").
                foreignField("_id").
                as("studProfile");
 
        Aggregation agg = Aggregation.newAggregation(projectUserAndBeneficiaries, unwindBeneficiars,  
                projectUserAndOtherDetails
                ,lookupOperation);
        AggregationResults<UserAndStudentData> output 
          = mongotemplate.aggregate(agg, "UserData", UserAndStudentData.class);

      

示例输出

Output in db shell
{
    "_id" : ObjectId("5d2f08574de2690001c281ac"),
    "username" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="503b2362646110373f3f7e333f3d" rel="noreferrer noopener nofollow">[email protected]</a>",
    "beneficiaries" : {
        "studentId" : "5d2f0e9c3bcf3e0001a7e562",
        "mcBeneficiaryId" : "597418",
        "enabled" : true
    },
    "beneficiaryStudId" : ObjectId("5d2f0e9c3bcf3e0001a7e562"),
    "studProfile" : [ 
        {
            "_id" : ObjectId("5d2f0e9c3bcf3e0001a7e562"),
            "lastName" : "Sharma",
            "firstName" : "Kapil",
            "studentRegisterCustomFieldValues" : [ 
                {
                    "bcfdValue" : "One",
                    "bcfdName" : "Year"
                } 
            ],
            "gender" : "M",
            "merchantId" : "38788943"
        }
    ]
}

java中那样

如果我将 $toObjectId 添加到上面的 java 聚合投影查询并运行它,则 StudProfile 数组始终为空,它会生成与 db shell 相同的值。

最佳答案

Spring data 不支持少数类型的方法。这个问题可能也包含在其中。但我们可以使用这个解决方案。

Aggregation aggregation=newAggregation(
    p-> new Document("$project",
        new Document()
        .append("username","$username"),
        .append("beneficiaries","$beneficiaries)
        .append("beneficiaryStudId",
            new Document("$toObjectId","$beneficiaries.studentId")
        )
     )
 )

关于java - 如何在Java Mongodb投影操作中给出$ObjectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64953363/

相关文章:

java - 自动启动 JBoss 服务 (MBean)

java - Google Checkout/Wallet 可以设置两个通知回调吗?

java - Stacktrace 不包括我的类(class)。我应该怎么办?

mongodb - _id 上的重复文档(在 mongo 中)

node.js - 将 Google App Engine 上的 Mongoose 连接到 mlab

node.js - 在 mongodb node.js 中使用 $group 和 find

mongodb - 在 Mongo 2.6 和 Pymongo 2.7.1 上使用带有聚合查询的 maxTimeMS 参数

java - 在 Eclipse 中生成随机数时崩溃

node.js - 返回重复结果的 Mongoose 查询

swift - 使用未解析的标识符 'DefaultConflictHandlers'