Skip to main content

Endpoints for working with collections

VIDEO RESULT after completing all actions on the "SingleCollection" page.

image.png

To start creating this page, let's begin by creating the 4 endpoints.

1. /getCollectionById

To retrieve information from the "imageCollection" collection in MongoDB based on the collection _id, you can create an endpoint with the following format:

image.png

  • function 

In the function body, you can write code to retrieve information for an object whose _id matches the one passed from the UI, and where the deleted property is false.

Add the "objectid" library in the function node.

image.png

  • mongo in
    In the MongoDB In node, make sure to select "find" and specify the name of the "imageCollection" collection.

image.png

2. /getSingleCollectionImages

Receive an image by collection _id.

image.png

In the function body, you can write code to retrieve information for an object whose _id matches the one passed from the UI, and where the deleted property is false.

msg.payload = {
    collection: objectid(msg.payload.id),
    deleted: false
}
return msg;

Add the "objectid" library in the function node.

image.png

  • mongo out
    In the MongoDB In node, make sure to select "find" and specify the name of the "gallery" collection.

image.png

3. /updateCollectionName 

Updating name of collection.

image.png

  • function

In the function body, specify that you want to update the name of the object with the provided name from the UI for the object with the specified _id.

Here's an example of how you can update the name in the function:

msg.query = {
    _id: objectid(msg.payload._id)
}

msg.payload = {
    $set: {
        name: msg.payload.name
    }
}

return msg;

Add the "objectid" library in the function node.

image.png

  • mongo out
    In the MongoDB In node, make sure to select "update" and specify the name of the "imageCollection" collection.

image.png

 4. /deleteFromGallery

Deleting a single item from a collection.

image.png

  • function

In the function body, you can write code to update the "deleted" field to true for the object with the specified _id.

msg.query = {
    _id: objectid(msg.payload._id)
}

msg.payload = {
    $set: {
        deleted: true
    }
}

return msg;

Add the "objectid" library in the function node.

image.png

  • mongo out

image.png