If I have two collections: Users and Events. A user can have many events...
Users
{
Username: "Mark",
Password: "1234567"
}
Events
{
EventTitle: "Skiing",
EventDate: "20/2/2015"
}
I need to associate an event with a user based on his username in java. So what I did was searching for the user....
BasicDBObject query = new BasicDBObject("Username", "Mark");
DBCursor cursor = table.find(query);
and I searched for the event based on its title...
BasicDBObject query = new BasicDBObject("EventTitle", "Skiing");
DBCursor cursor = table.find(query);
I just need to know how to associate that event with the user, assuming that only one event and one user would result from the search queries. There is a lack of examples on the internet.
When working with NoSQL databases, stop thinking in SQL. Stop normalizing the data. Do not try to make the data relational. Duplicating data is perfectly fine. Sometimes desired.
You can for example put Username
, or User ObjectId
, or both in Event
document either in an array, or create one Event
document per User
/Event
combination.
All in all it really depends on what other queries you will be running.