Java 使用spring mongodb创建具有差异的ProjectOnOperation
我有一个投影,我必须将其转换为java,但我得到的结果不正确,就像在使用javascript的mongo db executer中一样Java 使用spring mongodb创建具有差异的ProjectOnOperation,java,spring,mongodb,Java,Spring,Mongodb,我有一个投影,我必须将其转换为java,但我得到的结果不正确,就像在使用javascript的mongo db executer中一样 { $project: {"userId": 1, "followingAndNotFollowingBack": { $setDifference: ["$following", "$follower"]}} } 到目前为止,我在Java中有以下内容: private static final ProjectionOp
{
$project: {"userId": 1, "followingAndNotFollowingBack": {
$setDifference: ["$following", "$follower"]}}
}
到目前为止,我在Java中有以下内容:
private static final ProjectionOperation PROJECTION_OPERATION = Aggregation.project(UserRelationships.FIELD_USER_ID)
.and(SetOperators.SetDifference.arrayAsSet(UserRelationships.FIELD_FOLLOWING_USER_IDS)
.differenceTo(UserRelationships.FIELD_FOLLOWER_USER_IDS))
.as(FOLLOWING_AND_NOT_FOLLOWED_BACK);
但每次我都得到一个null
有人看到问题了吗?
非常感谢请尝试以下选项。我使用了
differenceToArray
private static final ProjectionOperation PROJECTION_OPERATION = Aggregation.project(UserRelationships.FIELD_USER_ID)
.and(UserRelationships.FIELD_FOLLOWING_USER_IDS)
.differenceToArray(UserRelationships.FIELD_FOLLOWER_USER_IDS)
.as(FOLLOWING_AND_NOT_FOLLOWED_BACK);
另一个例子:-
db.experiments.aggregate(
[
{ $project: { A: 1, B: 1, inBOnly: { $setDifference: [ "$B", "$A" ] }, _id: 0 } }
]
)
Aggregation aggregate = Aggregation.newAggregation(
Aggregation.project("A", "B").and("B").differenceToArray("A").as("inBOnly").andExclude("_id"));
代码:-
db.experiments.aggregate(
[
{ $project: { A: 1, B: 1, inBOnly: { $setDifference: [ "$B", "$A" ] }, _id: 0 } }
]
)
Aggregation aggregate = Aggregation.newAggregation(
Aggregation.project("A", "B").and("B").differenceToArray("A").as("inBOnly").andExclude("_id"));
感谢您的帮助,但问题是,我在测试和生活中使用了不同的MongoDB
因此,在一个版本中,带有null的differencetoarray为null,而在另一个版本中,它给出了正确的答案,因此,如果我与null数组联合,我必须添加信息以返回所有值