0

I have following records in Mongo 2.4,

    {
    "name": "AsiaEurope",
    "events": [{
    "action": "updated",
    "someHeaderField": "field1",
    "version":"3.3",
    "eventValue": {
        "local": "newZealand",
        "remote": "India"
    }
    }, {
    "action": "new",
    "someHeaderField": "field1",
    "version":"3.3",
    "eventValue": {
        "local": "Japan",
        "remote": "England"
    }
    }, {
    "action": "finished",
    "someHeaderField": "field1",
    "version":"3.2",
    "eventValue": {
        "local": "Brasil",
        "remote": "Turkey",
        "middlebox": "Russia"
    }
    }, {
    "action": "updated",
    "someHeaderField": "field1",
    "version":"3.2",
    "eventValue": {
        "local": "Canada",
        "remote": "Cuba"
    }
    }]
}

I want to write a Mongo query to get eventValue in Java where action equals to updated and name is AsiaEurope. There could be multiple elements in the Events (array) whose action equals to updated.

How to write the query and how to fetch EventValue in Java?

Thank you in advance!

5
  • 1
    you want to get output like : db.collection.find({"name":"AsiaEurope","events.action":"updated"},{"_id":0,"events.eventValue":1}) Commented Jun 9, 2015 at 11:27
  • try this in your shell. Converting it into java is not a big deal. Commented Jun 9, 2015 at 11:28
  • db.conference.aggregate([{$match: { name:"AsiaEurope"} }, {$unwind: "$events"}, {$match: {"events.action":"updated"} },{$group: {_id:"$_id", events:{$push:"$events"}}}]) Commented Jun 9, 2015 at 11:49
  • Above is the answer, I only need to study now how to using Java client to get the fields. By the way, How to format code in comment section? Commented Jun 9, 2015 at 11:51
  • a good reference is stackoverflow.com/questions/16752316/… Commented Jun 9, 2015 at 11:51

1 Answer 1

1

I did not tested it with mongo 2.4 (tested with mongo 3.0), but you can use something like following :

db.collection.aggregate({
    "$match": {
    "name": "AsiaEurope"
    }
}, {
    "$unwind": "$events"
}, {
    "$match": {
    "events.action": "updated"
    }
}, {
    "$project": {
    "_id": 0,
    "eventValue": "$events.eventValue"
    }
}).pretty()

And equivalent java code is :

    ArrayList<BasicDBObject> pipeline = new ArrayList<BasicDBObject>();

    BasicDBObject match = new BasicDBObject("$match", new BasicDBObject("name","AsiaEurope" ));
    BasicDBObject unwind = new BasicDBObject("$unwind", "$events");
    BasicDBObject match2 = new BasicDBObject("$match", new BasicDBObject("events.action","updated"));
    BasicDBObject project = new BasicDBObject("$project", new BasicDBObject("eventValue","$events.eventValue").append("_id",0));

    pipeline.add(match);
    pipeline.add(unwind);
    pipeline.add(match2);
    pipeline.add(project);
    AggregationOutput cursor = collection.aggregate(pipeline);

This will give you expected output.

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.