1

I try to write application in Java and Spring Data MongoDB.

My document looks:

@Data
@ToString
public class SomeDocument {
    private UUID id;
    private String name;
}

Repository:

@Repository
public interface SomeDocumentMongoRepository extends MongoRepository<SomeDocument, UUID> {
}

It's very simple. And I saved the document:

{
    "id": "5f4ac46b-55f7-4be4-b26f-2ca041334bec",
    "name": "some name"
}

Then I tried to read it from database using simple query db.someDocument.find() and I've got the result:

{ "_id" : BinData(3,"5Ev3VWvESl/sSzNBoCxvsg=="), "name" : "some name", "_class" : "org.springmongodemo.repo.SomeDocument" }

So my questions are:

  1. How to find in mongo shell document by _id using given UUID?
  2. What does mean number 3 in BinData(3,...)?
user
  • 4,410
  • 16
  • 57
  • 83
  • Do you need to use UUIDs? Can you not perhaps switch to ObjectId? https://docs.mongodb.com/manual/reference/method/ObjectId/ – dnickless Oct 05 '18 at 10:03

1 Answers1

0
  1. To find the document you'd have to encode your UUID into base64. Please see this question.

  2. "3" is the identifier for legacy UUID data format. For more please see here.

If you want to query from in-app you should use Spring Data's features, but from your question I assume you want to know how to query using mongo native tools.

Nestor Sokil
  • 2,162
  • 12
  • 28