Tag Archives: mongoose

Using mongoose

1-n, 1-1

https://stackoverflow.com/questions/32038793/one-to-one-relationship-with-mongoose

First of all, there is a huge difference between relationships in MongoDB and those in SQL based datastores (you need to be clear about this from the get-go).

Relationships in MongoDB are just representations of related data. There is no mechanism which maintains the integrity of these relationships.

What mongoose does with refs is just use the field having the ref option to query the _id field of documents in the referenced collection. This is used for operations like populate (which internally calls findById queries on the target collection and replaces the referenced field with the documents).

This being cleared, you can store one or many IDs for the referenced collection in the field, thus creating one-to-one or one-to-many “relationships”.

Add the event

Open up postman. select POST. In the the url, type in http://localhost/events

In Node express code, we have stated that for post verbs at url /events, we want it to hit create_event function. And create_event function is where we implement getting the data from the body object, and saving it into the database.

body:
postman-add-event

Then check the database to verify.

Look at the object id 5a2f3f9b8f4a4a4367dbf59e. We use this as the event id that we will be adding attendees to.

Add attendee(s) to the event (1 to n)

In order to register the attendee, we use postman.
given the route code:

we do the following.

web verb: POST
url: http://localhost/events/5a2f3f9b8f4a4a4367dbf59e

Notice that the last param is a phoneId. We just give a phone number like so:

url: http://localhost/events/5a2f3f9b8f4a4a4367dbf59e/13510083852

When the user put this in the browser, it will hit the registerAttendee function in node express. The registerAttendee function will look for name and phone in its request object’s body:

Hence, as we can see, it gets the properties phone and name from the body, create an object out of it, and pushes it onto the attendees’ array property.

So in your post man, click on body, highlight x-www-form-urlencoded, and enter key/values:

name: Hannah Hong
phone: 135103483252

Essentially, we are entering the properties that are needed by the eventAttendee schema:

Once you press Send on postman, the attendee should be in. Check the database:

> db.funEvents.find()

result:

{ “_id” : ObjectId(“5a2f3f9b8f4a4a4367dbf59e”),
“Event_Title” : “Super Badminton”,
“Event_Description” : “Play some badminton with fellow friends”,
“Author” : “Ricky T”,
“attendees” : [ { “phone” : “135103483252”, “name” : “Hannah Hong”, “registered” : false, “_id” : ObjectId(“5a2f405a8f4a4a4367dbf59f”) } ],
“Created_Date” : ISODate(“2017-12-12T02:31:55.180Z”),
“Event_Location” : null,
“Event_Date” : ISODate(“2017-12-12T02:31:55.180Z”), “__v” : 1 }

You will see that the attendees array now has the user Hannah Hong.

You can add as many users as you like as this is 1 to n. One event, many users.

Adding the location (1 to 1)

Adding the location to our event involves hitting the url at /events/:eventId
with a PUT verb:

eventListRoutes.js

We put the event id at the end of the url like so: http://localhost/events/5a2f3f9b8f4a4a4367dbf59e

web verb: PUT
url: http://localhost/events/5a2f3f9b8f4a4a4367dbf59e

postman-add-location

We add the information for the location via the body through

body => x-www-form-urlencoded:

address:132 Children’s park, hai yue, Shekou
city:Shenzhen
province: Guangdong
zip: 2847384

hit the “Send” button.

Finally, check your database:

> db.funEvents.find()

result:

{ “_id” : ObjectId(“5a2f3f9b8f4a4a4367dbf59e”),
“Event_Title” : “Super Badminton”,
“Event_Description” : “Play some badminton with fellow friends”,
“Author” : “Ricky T”,
“attendees” : [ { “phone” : “13510083852”, “name” : “Hannah Hong”, “registered” : false, “_id” : ObjectId(“5a2f405a8f4a4a4367dbf59f”) } ],
“Created_Date” : ISODate(“2017-12-12T02:31:55.180Z”),
“Event_Location” : { “_id” : ObjectId(“5a2f4195a713254374cbceee”), “zip” : “2847384”, “province” : “Guangdong”, “city” : “Shenzhen”, “address” : “132 Children’s park, hai yue, Shekou” },
“Event_Date” : ISODate(“2017-12-12T02:31:55.180Z”), “__v” : 1 }

Further References

Installing Mongoose


npm install mongoose –-save

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

/model/person.js

/app.js

Find all People

Find one person

Removing Users