Search code examples
javamongodbbson

Search for a value inside a document [MongoDB]


I'm trying to find a value inside a document in MongoDB, coded in JAVA, I'll give an example:

Main document:

date: 12.2
rate: 9
people:
   1:George
   2:Candice
   3: James
   4: John
   x:
   x+1:

This is really just in theory, the date and rate don't matter it's just an example.

Let's say I have the people column, and inside of it a new document with 1,2,3,4,x... infinitely, how would I be able to search inside the people column for a name without specifying the number in MongoDB?


Solution

  • You can try this https://docs.mongodb.com/manual/tutorial/query-arrays/ or you can use MongoTemplate in java and use find method to get list of people in your case.