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

时间:2015-08-04 20:51:32

标签: java mongodb morphia

我有UserProfile"名字","审核日志",...字段。我想在审核日志列中根据"first name", "activity On From", "activity On To"搜索集合。

 {
        "_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")
            }
        ]
    }

我该如何获取:

  

SELECT * FROM UserProfile WHERE" audit log.user id"包含(给定   搜索框中的名字)AND

为此我试过了:

    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();

此处输出:profileQuery{ "$and" : [ { "_id" : { "$oid" : "55bb9ff454ffd80ab4d32d8e"}} , { "audit log.user id" : { "$in" : [ { "$ref" : "userprofile" , "$id" : { "$oid" : "55bb9ff454ffd80ab4d32d8e"}}]}} , { "audit log.action" : { "$regex" : "Edit" , "$options" : "i"}} , { "audit log.activity on" : { "$gte" : { "$date" : "2015-08-03T06:00:00.000Z"}}} , { "audit log.activity on" : { "$lte" : { "$date" : "2015-08-05T06:00:00.000Z"}}}]}

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

0 个答案:

没有答案