Java 如何基于引用对象进行搜索

Java 如何基于引用对象进行搜索,java,mongodb,morphia,Java,Mongodb,Morphia,我有UserProfile和“名字”、“审核日志”,。。。领域。我想根据审核日志列中的“名字”、“活动开始于”、“活动开始于”搜索集合 { "_id" : ObjectId("55bb9ff454ffd80ab4d32d8e"), "first name" : "firstName0", "middle name" : "", "last name" : "lastName0", "audit log" :

我有
UserProfile
和“名字”、“审核日志”,。。。领域。我想根据审核日志列中的
“名字”、“活动开始于”、“活动开始于”
搜索集合

 {
        "_id" : ObjectId("55bb9ff454ffd80ab4d32d8e"),
        "first name" : "firstName0",
        "middle name" : "",
        "last name" : "lastName0",    
        "audit log" : [ 
            {
                "user id" : {
                    "$ref" : "userprofile",
                    "$id" : ObjectId("55bb9ff454ffd80ab4d32d90")
                },
                "action" : "Edited Date Of Birth",
                "activity on" : ISODate("2015-07-31T16:19:00.221Z")
            }, 
            {
                "user id" : {
                    "$ref" : "userprofile",
                    "$id" : ObjectId("55bb9ff454ffd80ab4d32d8e")
                },
                "action" : "Edited Gender",
                "activity on" : ISODate("2015-07-31T16:19:00.227Z")
            }
        ]
    }
我怎样才能这样取回:

从“audit log.user id”包含的用户配置文件中选择*(给定 搜索框中的名字)和

为此,我尝试:

    Datastore ds = getDatastore();

    Query<UserProfile> profileQuery = ds.createQuery(UserProfile.class);
    Query<UserProfile> auditQuery = ds.createQuery(UserProfile.class);

    profileQuery.field("_id").equal(userId);

    if (auditedBy != null) {
        auditQuery.field("first name").containsIgnoreCase(auditedBy);
        profileQuery.criteria("audit log.user id").in(auditQuery.asKeyList());
    }

    if (action != null) {
        profileQuery.field("audit log.action").containsIgnoreCase(action);
    }

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // Need to do Range for give 2 Dates
    if (activityOnFrom != null) {

        Date activityDateFrom = formatter.parse(activityOnFrom);
        profileQuery.field("audit log.activity on").greaterThanOrEq(
                activityDateFrom);
    }

    if (activityOnTo != null) {
        Date activityDateTo = formatter.parse(activityOnTo);
        profileQuery.field("audit log.activity on").lessThanOrEq(
                activityDateTo);
    }

    UserProfile q = profileQuery.get();
Datastore ds=getDatastore();
queryprofilequery=ds.createQuery(UserProfile.class);
Query auditQuery=ds.createQuery(UserProfile.class);
profileQuery.field(“_id”).equal(userId);
if(auditedBy!=null){
auditQuery.field(“名字”).containsIgnoreCase(auditedBy);
(auditQuery.asKeyList())中的profileQuery.criteria(“audit log.user id”);
}
如果(操作!=null){
profileQuery.field(“audit log.action”).containsIgnoreCase(action);
}
SimpleDataFormat格式化程序=新的SimpleDataFormat(“yyyy-MM-dd HH:MM:ss”);
//需要为给定的2个日期设置范围
if(activityOnFrom!=null){
日期activityDateFrom=格式化程序.parse(activityOnFrom);
profileQuery.field(“审核日志.activity on”).greaterThanOrEq(
activityDateFrom);
}
如果(ActivityOn!=null){
日期activityDateTo=formatter.parse(ActivityTo);
profileQuery.field(“审核日志.activity on”).lessThanOrEq(
活动日期);
}
UserProfile q=profileQuery.get();
此处输出:
profileQuery
{“$and”:[{“$id”:{“$oid”:“55bb9ff454ffd80ab4d32d8e”},{“审计日志.用户id”:{“$in”:[{“$ref”:“用户配置文件”,“$id”:{“$oid”:“55bb9ff454ffd80ab4d32d8e”}},{“审计日志.操作”:{“$regex”:“编辑”,“$options”:“i”},{“审计日志.活动日期”{gte:$2015-0006:00}“审核日志.activity on”:{“$lte”:{“$date”:“2015-08-05T06:00:00.000Z”}}}}]}


然而
UserProfile q
包含所有审核日志用户列表!(没有任何过滤器)。我希望获得ObjectId的审核用户详细信息:55bb9ff454ffd80ab4d32d8e不是两者都有!

不知道整个Java代码在做什么(这看起来非常复杂),但方法看起来是正确的。查询应该更简单:
”audit log.user id:{“$in”:[ObjectId”(“55bb9ff454ffd80ab4d32d90”)}
。您到底期望什么?看起来您正在返回结果…Nope@evanchooly:我期望只获取用户为55bb9ff454ffd80ab4d32d90的auditlog,但它返回所有可用的审核日志,而不关心的筛选条件”审核人“这是MongoDB的正常行为。当查询与数组的一个元素匹配时,将返回整个文档。如果只想获取触发匹配的数组项,则需要向查询提供请求字段的列表,并在数组字段上使用。然而,我不知道如何使用Morphia,因为我从未使用过它。@Philipp:我认为这就是问题所在。任何人都知道如何从数组列表中的所有可用对象中只提取条件对象。我不知道整个Java代码在做什么(这看起来非常复杂),但这种方法看起来是正确的。查询。但是,应该更简单:
“audit log.user id”:{“$in”:[ObjectId”(“55bb9ff454ffd80ab4d32d90”)}
。您到底期望什么?看起来您正在返回结果…不@evanchooly:我只希望使用用户55bb9ff454ffd80ab4d32d90获取审核日志,但是它返回所有可用的审核日志,而不关心“auditedBy”的过滤条件,这是MongoDB的正常行为。当查询与数组的一个元素匹配时,将返回整个文档。如果只想获取触发匹配的数组项,则需要向查询提供请求字段的列表,并在数组字段上使用。然而,我不知道如何使用Morphia,因为我从未使用过它。@Philipp:我想这就是问题所在,任何人都知道如何从数组列表中的所有可用对象中只提取条件对象