1

I am new to Mongodb. I have the following dataset in mongodb.

{
"_id": {
    "$oid": "563644f44b17ca12886440a9"
},
"data": [
    {
        "uid": 1,
        "character": " ",
        "unicode": 32,
        "color": -7309587
    },
    {
        "uid": 2,
        "character": "!",
        "unicode": 33,
        "color": -8911704
    },
    {
        "uid": 3,
        "character": "\"",
        "unicode": 34,
        "color": -1778539
    }

I am trying to retrieve the color from this field using the _id and character. I am not able to execute the query.

This is what I tried.

DBObject clause1 = new BasicDBObject("_id",new ObjectId(KEY1));  
                DBObject clause2 = new BasicDBObject("data.character",text[i]);    
                BasicDBList or = new BasicDBList();
                or.add(clause1);
                or.add(clause2);
                DBObject query = new BasicDBObject("$and", or);

Also I am having a lot of issues in finding ways to query in mongodb-java for the 3.0.0+ api. Could someone please help?

4
  • 1
    As an output you just need data.color for a particular _id & data.character. Right? Commented Nov 2, 2015 at 5:20
  • then, I guess you need to unwind data array and using $match and $projection operator in aggregation you can achieve this. Commented Nov 2, 2015 at 5:45
  • 1
    Can you please try this on shell. If this works, you can easily convert in into corresponding Java Code: db.col1.aggregate([{$unwind:"$data"},{$match:{$and:[{"_id" : ObjectId("<your id here>")},{"data.character":" "}]}},{$project:{"data.color":1,"_id":0}}]) Commented Nov 2, 2015 at 5:52
  • This works perfectly in the shell. Thank you Commented Nov 2, 2015 at 8:16

2 Answers 2

1
    MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
    MongoDatabase db = mongoClient.getDatabase("testDB");
    AggregateIterable<Document> iterable = db.getCollection("testCollection").aggregate(
            asList(new Document("$unwind", "$data"), new Document("$match", (new Document("_id", new ObjectId(
                    "5636f106b2acf98ecb033b98")).append("data.character", " "))), new Document("$project",
                    new Document("data.color", 1).append("_id", 0))));

    iterable.forEach(new Block<Document>()
    {
        @Override
        public void apply(final Document document)
        {
            System.out.println(document.toJson());
        }
    });

For more details check MongoDB Documentation.

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

1 Comment

I got this. I was facing a lot of trouble with aggregate method of collection. I was using the 3.0.0 java-mongo driver api. Apparently, they have changed it a lot and it's still in beta. Thank you so much.
0

A simple solution

In your document, oid is an unique key for each document and hence your query should be like this

  DBObject query = new BasicDBObject("_id.oid", KEY1);
  // Query with value - DBObject query = new BasicDBObject("_id.oid","563644f44b17ca12886440a9");

  Assign this to a cursor as shown below
  DBCollection coll = db.getCollection("mycollection");
  DBCursor cursor = coll.find(query);
  Iterate the collection and retrieve your desired value.

1 Comment

I am getting the id by _id also.

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.