11

I am new to MongoDB. My sample document is

{
    "Notification" : [
        {
            "date_from" : ISODate("2013-07-08T18:30:00Z"),
            "date_too" : ISODate("2013-07-30T18:30:00Z"),
            "description" : "fdfd",
            "url" : "www.adf.com"
        },
        {
            "date_from" : ISODate("2013-07-01T18:30:00Z"),
            "date_too" : ISODate("2013-07-30T18:30:00Z"),
            "description" : "ddddddddddd",
            "url" : "www.pqr.com"
        }
    ],

I am trying to update the Notification whose "url" : "www.adf.com". My Java code to do this is:

BasicDBObject query=new BasicDBObject("url","www.adf.com");

DBCursor f = con.coll.find(query);

It does not search for the document whose "url" is "www.adf.com".

5
  • con is connection object Commented Jul 26, 2013 at 6:02
  • coll is DBCollection object. Commented Jul 26, 2013 at 6:23
  • You forgot the Notification in your query. You have to query for Notification.url. Commented Jul 26, 2013 at 6:46
  • @obecker Notification is array not nested doc. I also tried it BasicDBObject query=new BasicDBObject("Notification.url","www.adf.com"); Commented Jul 26, 2013 at 9:02
  • see docs.mongodb.org/manual/core/read-operations -> Arrays The nested object syntax should work also for arrays. Commented Jul 26, 2013 at 9:08

1 Answer 1

14

You have a nested document in this case. Your document has a field Notification which is an array storing multiple sub-objects with the field url. To search in a sub-field, you need to use the dot-syntax:

BasicDBObject query=new BasicDBObject("Notification.url","www.adf.com");

This will, however, return the whole document with the whole Notification array. You likely only want the sub-document. To filter this, you need to use the two-argument version of Collection.find.

BasicDBObject query=new BasicDBObject("Notification.url","www.example.com");
BasicDBObject fields=new BasicDBObject("Notification.$", 1);

DBCursor f = con.coll.find(query, fields);

The .$ means "only the first entry of this array which is matched by the find-operator"

This should still return one document with a sub-array Notifications, but this array should only contain the entry where url == "www.example.com".

To traverse this document with Java, do this:

BasicDBList notifications = (BasicDBList) f.next().get("Notification"); 
BasicDBObject notification = (BasicDBObject) notifications.get(0);
String url = notification.get("url");

By the way: When your database grows you will likely run into performance problems, unless you create an index to speed up this query:

con.coll.ensureIndex(new BasicDBObject("Notification.url", 1));
Sign up to request clarification or add additional context in comments.

9 Comments

When I tried to access "url" seprated by above query String s=(String) f.curr().get("url"); It returns null value
@PrashantThorat That's likely because the returned document has no field url. It likely only has a field Notifications which contains a DBList which contains one DBObject which contains a field url. Check it in the debugger.
@PrashantThorat f.curr().get("Notifications").get(0).get("url") could work.
Is their any other alternative? I tried it but it gives error at get(0)
@PrashantThorat Sorry, but I lost my tarot deck. Please use the debugger and tell me what document f.curr() returns.
|

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.