mongodb-cg-lib
v1.3.3
Published
Library to make basic MongoDB operations
Downloads
83
Maintainers
Readme
mongodb-cg-lib
1. Introduction
This code has the objective of perform some of the basic mongo database operations such as "FIND", "INSERT", "UPDATE" and "DELETE". Each option requires its corresponding parameters to complete the operation and as a result it'll be a message with the successful execution or an error message. As MongoDB is a NoSQL Database instead of the use of tables it uses documents (JSON format) and the attibutes corresponds to the columns in a SQL Database.
This library is useful to be added into N3xGen components developed by their owners to use the principal functions of mongodb database and to be processed in OIH based flows.
Within "mongodb-cg-lib" there are three additional libraries are used: mongodb: This library is used to create a connection with a mongo database and perform one of the main options "find", "insert", "update", "delete". The correct parameter definition is required to this library to work properly. utils-nxg-cg: is used to print the necessary logs and validate the information structure to be processed.
The next is an example of the final result in the case of the "find" option, it is a JSON structure with the content of the documents in the collection and filtered according with the parameters:
[
{
"_id": "6304f50ff1b8d8dd1984307e",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 1999,
"genre": "fiction",
"stars": 5,
"flag": 1
},
{
"_id": "6307ea79803903a6531e3f26",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 1999,
"genre": "fiction",
"stars": 5,
"flag": 1
},
{
"_id": "6307ea79803903a6531e3f28",
"id": "tt0246006",
"title": "The Prestige",
"actor": "Christian Bale",
"year": 2006,
"genre": "Suspense",
"stars": 4,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f31",
"id": "tt0238091",
"title": "The Silence of the Lambs",
"actor": "Jodie Foster",
"year": 1991,
"genre": "suspence",
"stars": 4,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f32",
"id": "tt0246006",
"title": "The Prestige",
"actor": "Christian Bale",
"year": 2006,
"genre": "Suspense",
"stars": 4,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f30",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 1999,
"genre": "fiction",
"stars": 5,
"flag": 1
}
]
In the case of the "insert", "update" or "delete" options the result is a message according with the operation:
"3 documents were inserted"
"Updated 3 documents"
"Deleted 3 documents"
2. Methods explanation
2.1. mongoProcess
The library can be installed from npm page with the next sentences:
npm install mongodb-cg-lib
, npm i mongodb-cg-lib
or yarn install mongodb-cg-lib
This method is used to perform the four main operation to interact with a mongo database such as "find", which performs a search for documents in a specific collection and returns all the matches to the input parameters. The next one is "insert", which receives as main parameter "content" which has to be a JSON in base64. The next one is "update" which allows to update a database collection with the parameters set. The last one is "delete" to remove documents from a database collection according with the parameters.
3. Argument and result explanation
Arguments: The only and main method needs a list of parameters, some are mandatory and others are optional, then all the parameters are listed with their description:
- url(required): This is the url to specify the mongo database connection.
- crud(required): It refers to the option to process information to the database:
- find: It refers the operation to extract information from the database, it is equivalent to the "SELECT" sentens in SQL database. Adding additional parameters allows to filter the data to be extracted.
- insert: This option allows to add new documents of a collection into the database, this option requires other parameters to work properly. The library accepts base64 data to be inserted.
- update: This option allows to changethe the information in the documents of a collection, this option requires other parameters to specify the attributes to be changed and which documents will be updated..
- delete: This option allows to remove some/all documents in a collection, some other parameters are required to filter the documents required to be removed.
- database(required): It is the name of the database to establish the connection.
- col(required): It is the name of the collection in which execute the transactions.
- filter(required): This parameter is required to be combined with 3 options of the "crud" parameter. It should be used with "find" to filter exactly de documents required, "update" to set in which documents the change will be applied, "delete" to set a filter allowing to remove only some specific documents.
- updateData(no required on find, insert or delete): It refers to an object used for the "update" option in the "crud" property to process the data that will replace the information in a document.
- content (no required on find, update or delete): This parameter allows to send an array of JSON objects to perform the action to add new documents into the database, this object type is required because the information in the Mongo database are JSON objects (the documents) and this JSON content need to has a good format because de library does the validation of this.
Result: The final result varies depending on the process, if it is "find" it will return a JSON as a result with the documents according with the filter set, if it is an "insert", "update" or "delete " operation the result will be a message indicating that the operation was successful or it was an error.
The next is an example of a result of a "find" operation.
[
{
"_id": "6304f50ff1b8d8dd1984307e",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 1999,
"genre": "fiction",
"stars": 5,
"flag": 1
},
{
"_id": "6307ea79803903a6531e3f26",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 1999,
"genre": "fiction",
"stars": 5,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f32",
"id": "tt0246006",
"title": "The Prestige",
"actor": "Christian Bale",
"year": 2006,
"genre": "Suspense",
"stars": 4,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f30",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 1999,
"genre": "fiction",
"stars": 5,
"flag": 1
}
]
The next corresponds to results of the "insert", "update" and "delete" operations:
"3 documents were inserted"
"Updated 3 documents"
"Deleted 3 documents"
3. Examples
This part shows some examples on how to send the input parameters to the library and what its result would be. In the next object it is a valid object with full parameters set to use the library:
{
"url":"mongodb+srv://my-mongodb-url-example",
"crud":"find",
"database":"my6DmDB",
"col":"movies",
"filter":{}
}
In this case the "find" is used and is set to get all the documents of the collection (it will return all the documents of the collection because there is no filter configured), so the result is gonna be as follows (JSON will be the format of object returned):
[
{
"_id": "6304f50ff1b8d8dd1984307e",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 2000,
"genre": "fiction",
"stars": 5,
"flag": 1
},
{
"_id": "6307ea79803903a6531e3f26",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 2000,
"genre": "fiction",
"stars": 5,
"flag": 1
},
{
"_id": "6307ea79803903a6531e3f28",
"id": "tt0246006",
"title": "The Prestige",
"actor": "Christian Bale",
"year": 2006,
"genre": "Suspense",
"stars": 4,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f31",
"id": "tt0238091",
"title": "The Silence of the Lambs",
"actor": "Jodie Foster",
"year": 1991,
"genre": "suspence",
"stars": 4,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f32",
"id": "tt0246006",
"title": "The Prestige",
"actor": "Christian Bale",
"year": 2006,
"genre": "Suspense",
"stars": 4,
"flag": 1
},
{
"_id": "630900d5803903a6531e3f30",
"id": "tt0133093",
"title": "The Matrix",
"actor": "Keanu Reeves",
"year": 2000,
"genre": "fiction",
"stars": 5,
"flag": 1
}
]
In the "insert" operation the next parameters are set to add new documents to the collection (this example use base64 encoding, but could also be inserted with JSON objects, if decoded two documents should be visible):
{
"url":"mongodb+srv://my-mongodb-url-example",
"crud":"insert",
"database":"my6DmDB",
"col":"movies",
"filter":{},
"content":"WwogICAgewogICAgICAgICJfaWQiOiAiNjMwNGY1MGZmMWI4ZDhkZDE5ODQzMDdlIiwKICAgICAgICAiaWQiOiAidHQwMTMzMDkzIiwKICAgICAgICAidGl0bGUiOiAiVGhlIE1hdHJpeCIsCiAgICAgICAgImFjdG9yIjogIktlYW51IFJlZXZlcyIsCiAgICAgICAgInllYXIiOiAyMDAwLAogICAgICAgICJnZW5yZSI6ICJmaWN0aW9uIiwKICAgICAgICAic3RhcnMiOiA1LAogICAgICAgICJmbGFnIjogMQogICAgfSwKICAgIHsKICAgICAgICAiX2lkIjogIjYzMDdlYTc5ODAzOTAzYTY1MzFlM2YyNiIsCiAgICAgICAgImlkIjogInR0MDEzMzA5MyIsCiAgICAgICAgInRpdGxlIjogIlRoZSBNYXRyaXgiLAogICAgICAgICJhY3RvciI6ICJLZWFudSBSZWV2ZXMiLAogICAgICAgICJ5ZWFyIjogMjAwMCwKICAgICAgICAiZ2VucmUiOiAiZmljdGlvbiIsCiAgICAgICAgInN0YXJzIjogNSwKICAgICAgICAiZmxhZyI6IDEKICAgIH0KXQ=="
}
The result of the insert operation should be something like this:
"2 documents were inserted"
In the "update" operation the parameters set allows to indicate the information to be replaced and in which documents this change will be performed:
{
"url":"mongodb+srv://my-mongodb-url-example",
"crud":"update",
"database":"my6DmDB",
"col":"movies",
"filter":{"year":1999},
"updateData":{"title":"My new title"}
}
In this example the title of all the movies in the collection that has year as "1999" will be replaced with "My new title". The result is going to be something like this:
"Updated 3 documents"
In the "delete" operation it is only required to define which documents will be removed:
{
"url":"mongodb+srv://my-mongodb-url-example",
"crud":"delete",
"database":"my6DmDB",
"col":"movies",
"filter":{"year":1999},
}
In this example all the movies in the colection that has the year as "1999" will be removed. The result is going to be something like this:
"Deleted 3 documents"