1

I have a mongo collection as follows:

    {
    "id": "510aec8e8426e96c156a0946",  
    "name": "ABC",
    "categoryToShardMap": null
    },
    {
    "id": "510aecd284268e0f60e547a5",
    "name": "XYZ",
    "categoryToShardMap": {
    "shoes": 1
    },
    {
    "id": "510aecbd84268e0f60e547a4",
    "name": "AAA",
    "categoryToShardMap": {
     "shoes": 3,
     "jeans": 2
     }

Where I have declared field 'categoryToShardMap' as :

    private HashMap<String, Long> categoryToShardMap;

I am trying to create a REST Api where if someone provides the shardId, he should get back the mongo document which contains it. For example: if I pass shardId = 3, I should receive the mongo document:

    {
    "id": "510aecbd84268e0f60e547a4",
    "name": "AAA",
    "categoryToShardMap": {
    "shoes": 3,
    "jeans": 2
    }

I am trying to use java to do this. But I am not sure how should I query this using mongotemplate or DBObject. Can someone point to me to some examples on how to query if a map stored in mongo document contains a specified value?

1 Answers1

0

You can do this by $ne operator. Just check whether the field is not null.

Here is an example how you can achieve this using javadriver.

new BasicDBObject("categoryToShardMap", new BasicDBObject("$ne", null));

Or you can use $where operator to check the length of the array. This is explained in the following post:

Query for documents where array size is greater than 1

If you are looking for a specific value in a map you can use the following command:

DBObject query = BasicDBObjectBuilder.start().add("categoryToShardMap.shoes", 3).get(); // query for getting the documents whose "categoryToShardMap" field contains shoes with value 3
Community
  • 1
  • 1
cubbuk
  • 7,800
  • 4
  • 35
  • 62
  • This helps in finding whether map is null or not. But if I want to check if value '3' exists in the map this approach won't work. – Soumyakant Mishra Feb 04 '13 at 17:02
  • I edited my question as it was not clear earlier if I am looking for a specific value in the map. – Soumyakant Mishra Feb 04 '13 at 17:06
  • I updated my answer according to your changes. I would suggest you to take a look at https://github.com/jmkgreen/morphia which is a Object document mapper for Mongo in Java which eases using Mongo with Java. – cubbuk Feb 04 '13 at 17:38
  • The updated answer is close to what I want. But the problem is field name could be anything. I will only get the value to find if it is present in the map. – Soumyakant Mishra Feb 04 '13 at 17:51
  • If the key is not part of the map, the above query will also not return anything. Whats your use case here, why do you first need to check whether the field exists? Why don't you just check whether the value for that field exist? – cubbuk Feb 04 '13 at 17:55
  • My use case is: Every doc will store a category to shard Id mapping. Shard Id would be globally unique across all docs. So when a user searches the collection by shard Id, he should get back a doc which contains a map containing that shard Id. The solution you presented requires user to know what category he's looking for. – Soumyakant Mishra Feb 04 '13 at 18:26
  • 1
    Can you update your question and specify these requirements. Please also add two different document showing us what exactly you are querying for. As far as I understood your shard id will be a key of your "categoryToShardMap". Lets assume that shard id is 1, then you can search for it using notation similar to following "categoryToShardMap.1" instead of categoryToShardMap.shoes. So you can actually make queries on dynamic field names. But as I said earlier please update your question and give some real documents that you will query on. – cubbuk Feb 04 '13 at 18:38
  • 1
    I followed your suggestion. I used shard id as key instead of category and then this query: new BasicDBObject("shardToCategoryMap." + shard, new BasicDBObject("$ne", null)); It works now! Thanks for your help. – Soumyakant Mishra Feb 04 '13 at 20:15