0

I have a Mongo DB COllection as shown

{
    "name": "SAM",
    "collection": [
        {
            "date": "2013-03-16",
            "values": [
                {
                    "price": "24.0"
                }
            ]
        },
        {
            "date": "2013-04-20",
            "values": [
                {
                    "price": "10.0"
                }
            ]
        },
        {
            "date": "2013-05-18",
            "values": [
                {
                    "price": "12.0"
                }
            ]
        },
        {
            "date": "2013-06-22",
            "values": [
                {
                    "price": "10.0"
                }
            ]
        },
        {
            "date": "2013-09-21",
            "values": [
                {
                    "price": "38.0"
                }
            ]
        }
    ]
}

I was trying to get data related to a Particular date (2013-03-16) as shown But i am getting data of all the dates .

This is what i tried .

Please tell me where i am doing mistake .

package com;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;

public class Test {

    public static void main(String args[]) throws UnknownHostException {
        Mongo mongo = new Mongo();
        DB db = mongo.getDB("test");
        DBCollection mycollection = db.getCollection("mycollection");

        BasicDBObject query = new BasicDBObject();
        query.put("name", "SAM");
        query.put("collection.date", "2013-03-16");

        DBCursor cursor = mycollection.find(query);

        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

    }

}
3
  • MongoDB will return the entire record not a subset of a record when queried. Your "collection" above looks to be a single record. You could try breaking this up into individual records, each with name, date and values. Commented Mar 1, 2013 at 19:37
  • If you need to use the embedded array for your schema, you can use the $unwind operator from the Aggregation Framework to break up the single document into a document per entry in the embedded array. Commented Mar 1, 2013 at 19:41
  • Thanks for the response , but i cannot change the Structure of Database . So you mean to say that it is not possible to avoid entire record from returning. Commented Mar 1, 2013 at 19:45

2 Answers 2

1

You can use the $ positional projection operator to do this in the field selection parameter of find:

BasicDBObject query = new BasicDBObject();
query.put("name", "SAM");
query.put("collection.date", "2013-03-16");
BasicDBObject fields = new BasicDBObject();
fields.put("name", 1);
fields.put("collection.$", 1);

DBCursor cursor = mycollection.find(query, fields);
Sign up to request clarification or add additional context in comments.

3 Comments

This is working , but could you please tell me why you have hardcoded 1 for name and collection keys ??
@PreethiJain Because the value of the keys in the field selection parameter is determines whether the field is included in the result; 1 (or any truthy value) for included, 0 for excluded.
what i understood is that the fileds name and collection.$ will be part of result ?? Am i right . please tell me
0

You are trying to query an element inside the array collection, you need to use $elemMatch, instead of,

query.put("collection.date", "2013-03-16");

you can try with

query.put("collection", new BasicDBObject("$elemMatch", new BasicDBObject("date","2013-03-16")));

Read more about it here: http://docs.mongodb.org/manual/reference/projection/elemMatch/

2 Comments

Thanks Vikas, i tried but still i am getting all the Dates as response .
As JohnnyHK mentioned in his code, you also need to pass new BasicDBObject("collection.$", 1); in the find method to get a particular entry, here is the doc for $ operator: docs.mongodb.org/manual/reference/projection/positional/#_S_

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.