0

I have a list of documents like this saved in MongoDB:

{
    "_id" : "f9f44e87-4260-4699-99a6-af6b58b4fb7e",
    "address" : {},
    "lastUpdate" : "1414083484838",
    "listvalues" : {},
    "userModels" : {
        "5067" : {            
            "generated_date_timestamp" : NumberLong(1413985161866),
            "model_id" : 5067,
            "score_id" : {
                "0" : {
                    "score_level_id" : 5267,
                    "confidence" : 0,
                    "generated_timestamp" : NumberLong(1413985161866)
                }
            },
            "points" : 0,
            "delta_points" : 0,
            "override_model" : 0,
            "override_score" : 0,
        },
        "5079" : {
            "generated_date_timestamp" : NumberLong(1413985161866),
            "model_id" : 5079,
            "score_id" : {
                "0" : {
                    "score_level_id" : 5292,
                    "confidence" : 0,
                    "generated_timestamp" : NumberLong(1413985161866)
                }
            },
            "points" : 0,
            "delta_points" : 0,
            "override_model" : 0,
            "override_score" : 0,
        },
        "5080" : {
            "_class" : "com.mediresource.datatype.model.bean.Model",
            "generated_date_timestamp" : NumberLong(1413985161866),
            "model_id" : 5080,
            "score_id" : {
                "0" : {
                    "score_level_id" : 5294,
                    "confidence" : 0,
                    "generated_timestamp" : NumberLong(1413985161866)
                }
            },
            "points" : 100,
            "delta_points" : 0,
            "override_model" : 0,
            "override_score" : 0,
        }
    }
}

And I need to count how many "userModels", for all _id in mongoDB, has more that 0 points. What I'm saving here is a Java HashMap defined as UserModels<Integer, Model>.

This is the query I was trying to use, but without success:

db.anonProfile.find(
   { "userModels": { $elemMatch: { "points":  { $gte: 0 } } } }
).size();

Any clue?

6
  • 1
    Nothing in your data is actually an "array" which will display with bracket [] notation. These are just sub-documents or Hash/Map structures and this is why it does not work. You have some bad code producing this. The structure is not optimal for your purposes. Commented Oct 24, 2014 at 9:55
  • May I create some cursors to go through the data? Commented Oct 24, 2014 at 9:59
  • 1
    The point here is that you don't want "Hash/Map" structures the way you have used them. This is an "anti-pattern" which makes queries difficult along with many other operations. Data is data, you should not be using data-points as the names of keys. Do you want to know how to do it properly and solve your problem? Commented Oct 24, 2014 at 10:05
  • I understand that, put the point is, this is from production environment and I cannot change it. This query is only for report purpose. Commented Oct 24, 2014 at 10:43
  • 1
    Then you are left with the horrible option of JavaScript based traversal only, which is much slower than more efficient options. I think talking to the boss and showing these comments would be more productive than giving you a bad performing answer. Commented Oct 24, 2014 at 10:51

1 Answer 1

1

You can use it with spring-data mongodb like:

Criteria criteria=Criteria.where("userModels.points").gte(0));
Aggregration aggregration=newAggregration(unwind("userModels"),match(criteria),group("_id").count().as("param"));

But i can not understand why you are using the numbers under userModels.It is not good.Because of that i did not use the numbers under userModels.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.