4

I have read some forums and blog posts but did not find a good answer about MongoDB schema structure when referencing another collection.

For example, if I have a user collection, where the user belongs to only one address (his residence). But a given address may be the same for several users, where the address collection structure has the fields: zipcode, street, city, state, country. My question arises regarding in a collection structure where I can have millions of users that maybe or maybe not live in the same street, in this case it would be a better approach use the address embedded(denormalization approach) on the user collection, or using objectID to make a reference to the address collections.

bsantoss
  • 427
  • 6
  • 17

1 Answers1

0

it really depends on how your application will be querying the data. my personal rule of thumb however, is to not embed if there's going to be more than a few hundred entities/documents in there. also you have to keep in mind the 16mb per document size limit in mongodb. if there's a probability of millions of a certain type of entity getting created, it would be best to store them in their own collection and have references pointing to them from related entities.

here's a c# example of how i'd do it for the scenario you've given:

using MongoDB.Entities;
using System.Linq;

namespace StackOverflow
{
    public class Program
    {
        public class User : Entity
        {
            public One<Address> Address { get; set; }

            public string Name { get; set; }
        }

        public class Address : Entity
        {
            public Many<User> Users { get; set; }

            public string Street { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string ZipCode { get; set; }
            public string Country { get; set; }

            public Address() => this.InitOneToMany(() => Users);
        }

        private static void Main(string[] args)
        {
            // init connection
            new DB("test", "127.0.0.1");

            // create address
            var address = new Address
            {
                Street = "4616 Greenwood Pl",
                City = "Los Angeles",
                State = "CA",
                ZipCode = "90027",
                Country = "USA"
            }; address.Save();

            // create first user
            var user1 = new User
            {
                Name = "Amanda Woodward",
                Address = address.ToReference(),
            }; user1.Save();

            // create second user
            var user2 = new User
            {
                Name = "Billy Campbell",
                Address = address.ToReference(),
            }; user2.Save();

            // link the users to the address
            address.Users.Add(user1);
            address.Users.Add(user2);

            // find all users who live at a given address
            var result1 = DB.Queryable<User>()
                            .Where(u => u.Address.ID == address.ID)
                            .ToList();

            // find address of a given user
            var result2 = DB.Queryable<User>()
                            .Where(u => u.Name == "Amanda Woodward")
                            .Select(u => u.Address)
                            .First()
                            .ToEntity();

            // find a particular user at an address
            var result3 = address.Users.ChildrenQueryable()
                                       .Where(u => u.Name == "Billy Campbell")
                                       .First();

            // find all users living on a given street
            var addressIDs = DB.Queryable<Address>()
                               .Where(a => a.Street.Contains("Greenwood Pl"))
                               .Select(a => a.ID)
                               .ToArray();

            var result4 = DB.Queryable<User>()
                            .Where(u => addressIDs.Contains(u.Address.ID))
                            .ToList();
        }
    }
}
Dĵ ΝιΓΞΗΛψΚ
  • 5,068
  • 3
  • 13
  • 26