4

I'm completing a Java project in Eclipse as part of my university assignment. One of the requirements of the project is to write data to a text file and read it back in, in another class. I've decided, however, to use MongoDB rather than text files.

The format of the data looks like this:

Data

When I read the data back in from Mongo I use the following code:

MongoClientURI connectionString = new MongoClientURI("<My connection string>");
MongoClient mongoClient = new MongoClient(connectionString);

MongoDatabase database = mongoClient.getDatabase("Timeline");

MongoCollection<Document> collection = database.getCollection("HistoricalFigure");

MongoCursor<Document> cursor = collection.find().iterator();

try {
    while (cursor.hasNext()) {
        system.out.println(cursor.next().toJson());
    }
} finally {
    cursor.close();
    }

This works great and prints the following from my Mongo collection:

Result

(Ignore the data - Just chucked it in randomly)

I know similar questions have been asked in the past about removing _id fields from the results and so on - So apologies for that - But unfortunately, I haven't been able to apply these examples to my own code as they do vary quite a bit.

What I would like to achieve from this is to print to the console just the value of Historical Figure so that it prints this:

Desired outcome

If anybody could assist I would really appreciate it - I assume the answer will lie somewhere within the collection.find() but I'm just unsure how.

Many thanks, George

Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
George
  • 57
  • 1
  • 5

2 Answers2

2

Mongo Java driver v3.x provides a helpful projection shortcut for this: Projections.excludeId().

But that's just syntactic sugar over this: new BsonDocument("_id", new BsonInt32(0))

So, if you are using a Mongo Java driver version >= 3.x then just add this projection to your find() call:

collection.find().projection(Projections.excludeId()).iterator();

If you are using a Mongo Java driver version < 3.x then just add this projection to your find() call:

collection.find().projection(new BsonDocument("_id", new BsonInt32(0))).iterator();

This projection instructs Mongo to not include the _id attribute in any documents returned by the find call.

glytching
  • 44,936
  • 9
  • 114
  • 120
  • This answer is almost perfect thank you! I'm using 3.6 so the projection method works. However, this prints to my console `{ "Historical Figure" : "John Cleese (May 23 1749 - March 1 1823) - Maddog" }` - Is there anyway for my to simply print `John Cleese (May 23 1749 - March 1 1823) - Maddog`? – George Dec 13 '17 at 11:21
  • MongoDB is returning a `Document` to you, this document consists of a collection of key/value pairs (i.e. Historical Figure=John Cleese...) . This: `{ "Historical Figure" : "John Cleese (May 23 1749 - March 1 1823) - Maddog" }` is just one representation of that document. If you want to get the value without the key then use: `document.getString("Historical Figure")` but you cannot instruct MongoDB to return values without their keys – glytching Dec 13 '17 at 11:25
0

U can pass an object literal with id set to -1 and historical figure to 1.

Collection.find({},{'_id':0,'Historical figure':1})

KrishnaSingh
  • 696
  • 1
  • 7
  • 12
  • That's a 'raw' shell command, I _think_ the OP is asking how to express that using the Mongo Java driver. – glytching Dec 13 '17 at 11:18