title: Designing mongodb schema with searchable nested arrays.
body: <p>I'm new to Mongodb coming from relational databases and I'd also like to point out I'm using SpringBoot with JPA. If I were to build an automotive classified site where I would have thousands of Users and 100s of thousands of listings, how would I go about setting up the schema? I've read some articles that say normalizing nosql data is bad practices. </p>
  
  <p>Anyhow lets say we have the following structure. </p>
  
  <pre><code>User
    id
    name
    email
  
  Cars
    id
    make
    model
    year
  </code></pre>
  
  <p>I would need to be able to list many cars with the User and what i've seen in my examples is it creates a nested array of cars within User. This would work great for user accounts where I'd like to provide the user with all their cars </p>
  
  <p>Where I get a bit confused is with the cars. The cars need to be able to be searched very quickly and would not need the user info right away. In a sql db I would typically do a search against the cars (year, make, model) and grab the user later on if I needed it. </p>
  
  <p>In mongodb, do you create a User document that contains a nested car array? or do you somehow create 2 documents that are both automatically maintained and search against the car document for performance reasons?</p>
  
  <p>Sample code</p>
  
  <pre><code>@Document(collection = "person")
  public class Person {
  
      @Id
      private String id;
  
      private String firstName;
      private String lastName;
  
  //    @DBRef(lazy = true)
      private List&lt;Listing&gt; listings;
  
      public Person(String firstName, String lastName) {
          this.firstName = firstName;
          this.lastName = lastName;
      }
  }
  
  @Document(collection = "listing")
  public class Listing {
  
      @Id
      public String id;
  
      public String year;
      public String make;
      public String model;
      public String trim;
  
      public Listing(String year, String make, String model, String trim) {
          this.year = year;
          this.make = make;
          this.model = model;
          this.trim = trim;
      }
  }
  
  
  @Override
  public void run(String... args) throws Exception {
      repository.deleteAll();
  
      List&lt;Listing&gt; listings = new ArrayList&lt;&gt;();
      Listing listing = new Listing("2008", "Ford", "Focus", "SE");
  
      //listingRepository.save(listing);
  
      listings.add(listing);
  
      Person person = new Person("Alice", "Smith");
      person.setListings(listings);
      // save a couple of customers
      repository.save(person);
  
      person = new Person("Bob", "Smith");
  
      listings = new ArrayList&lt;&gt;();
      listings.add(new Listing("2018", "Chrysler", "300", "S"));
  
      person.setListings(listings);
      repository.save(person);
  
      // fetch all customers
      System.out.println("Customers found with findAll():");
      System.out.println("-------------------------------");
      for (Person _person : repository.findAll()) {
          System.out.println(_person);
      }
      System.out.println();
  
      // fetch an individual customer
      System.out.println("Person found with findByFirstName('Alice'):");
      System.out.println("--------------------------------");
      System.out.println(repository.findByFirstName("Alice"));
  
      System.out.println("Persons found with findByLastName('Smith'):");
      System.out.println("--------------------------------");
      for (Person _person : repository.findByLastName("Smith")) {
          System.out.println(_person);
      }
  
      List&lt;Listing&gt; _listings = listingRepository.findAll();
      System.out.println("listings " + _listings.size());
      _listings.forEach(v -&gt; {
          System.out.println(v.toString());
      });
  }
  </code></pre>