Create Collection
Let us search for the already existing databases in our Mongodb first. For this, we are going to open the MongoDB shell and run its “show dbs” instruction in the query area as provided. The output for this instruction shows 3 built-in databases with different sizes while we are working in a dummy “test” database.
admin 40.00 KiB
config 60.00 KiB
local 72.00 KiB
Using the “use” instruction along with the database name “test”, you can switch to any database as we have done below. The output shows that we are already using the “test” database.
already on db test
Let us display all the available collections of a “test” database via the “show collections” query. The output detail shows nothing as we have no collection right now.
We are creating a new collection named “Test” in the “test” database that will be used further. For this, we have tried the “db” query with the “createCollection” function of MongoDB. The output status “ok: 1” shows that the collection is created successfully.
{ ok: 1 }
Example # 01:
It is time to begin with our first example by adding some records to the “Test” collection that we have just created. To do so, we are going to use the “insert” function of MongoDB in the same “db” query along with the name of a collection “Test”. The record we have added contains 3 fields in total. Two of the fields are in array form i.e “PersonalDetails” and “AcademicRecord” contains subfields. The use of curly brackets shows the total number of records in a collection and the total number of fields within a specific field. Records have been added perfectly.
... AcademicRecord: [{Qualification: "BS", CGPA: 3.8}] })
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
acknowledged: true,
insertedIds: { '0': ObjectId("63b389fda7ac5549359eba32") }
}
To get all the records displayed on the MongoDB shell, we will be using the find() function within the same instruction after the collection name that also follows the forEach() function. The forEach() function prints the records in Json format as shown below.
{
_id: ObjectId("63b389fda7ac5549359eba32"),
Name: 'Tina',
PersonalDetails: [
{
Age: 44,
Email: 'tina@gmail.com'
}
],
AcademicRecord: [
{
Qualification: 'BS',
CGPA: 3.8
}
]
}
It is time to use the “size” operator to only display the record with a specific number of fields in it. So, we utilized it in the find() function to get the record where the PersonalDetails field has 2 separate field records in it, each record within the {} brackets. The output shows nothing because no record has 2 fields within the PersonalDetails field.
On setting the value of the “size” operator to 1 and executing the updated instruction, we have the whole record with its nested fields displayed on our MongoDB shell screen as below. The reason behind this display is clear: PersonalDetails has 1 record as per the {} brackets.
[
{
_id: ObjectId("63b389fda7ac5549359eba32"),
Name: 'Tina',
PersonalDetails: [ { Age: 44, Email: 'tina@gmail.com' } ],
AcademicRecord: [ { Qualification: 'BS', CGPA: 3.8 } ]
}
]
Example # 02:
Let us add another same-format record in the “Test” collection to see if the size operator works on multiple records or not. The same insert query was expected to add records for the person “Beba”. Although we have added more than one field within the PersonalDetails and AcademicRecord array, adding these fields within the one pair of curly brackets considers them as one record.
{
acknowledged: true,
insertedIds: { '0': ObjectId("63b38c57a7ac5549359eba33") }
}
After adding the second record, we tried the same query to display the records we have in the “Test” collection. Both the records were displayed in the standard format of arrays in MongoDB.
[
{
_id: ObjectId("63b389fda7ac5549359eba32"),
Name: 'Tina',
PersonalDetails: [ { Age: 44, Email: 'tina@gmail.com' } ],
AcademicRecord: [ { Qualification: 'BS', CGPA: 3.8 } ]
},
{
_id: ObjectId("63b38c57a7ac5549359eba33"),
Name: 'Beba',
PersonalDetails: [ { Age: 44, Email: 'tina@gmail.com', City: 'California' } ],
AcademicRecord: [ { Qualification: 'MS', CGPA: 3.9 } ]
}
]
Let us execute the “find” function query to display the records of a test collection while utilizing the value of the “$size” operator set to “3” that has been applied to the PersonalDetails field. As the PersonalDetails field does not contain more than 1 field for now, therefore the output is null.
Now, updating the value of the “$set” operator to 1 as demonstrated from the below query. The set value “1” demonstrates that the only record will be shown at the console screen where the “PeronalDetails” field has a single record in it. As both the main records we have added, hold single fields within the PersonalDetails field i.e. {}, both records are displayed.
[
{
_id: ObjectId("63b389fda7ac5549359eba32"),
Name: 'Tina',
PersonalDetails: [ { Age: 44, Email: 'tina@gmail.com' } ],
AcademicRecord: [ { Qualification: 'BS', CGPA: 3.8 } ]
},
{
_id: ObjectId("63b38c57a7ac5549359eba33"),
Name: 'Beba',
PersonalDetails: [ { Age: 44, Email: 'tina@gmail.com', City: 'California' } ],
AcademicRecord: [ { Qualification: 'MS', CGPA: 3.9 } ]
}
]
Example # 03:
Let us insert a new record in the “Test” collection via the “insert” function. This time, we choose to add 3 fields within the PersonalDetails field of this collection, while the field “AcademicRecord” contains 2 records within it. We will be applying the “size” operator on it.
{
acknowledged: true,
insertedIds: { '0': ObjectId("63b38d59a7ac5549359eba34") }
}
Now that we have added a nested record in the “Test” collection, we will be displaying it on the console screen using the find() function instruction. To display a specific record, we need to mention the name of any field followed by its value within a specific record i.e., to display the record where the “Name” field has its value “Paul”. Only the last added record was displayed.
[
{
_id: ObjectId("63b38d59a7ac5549359eba34"),
Name: 'Paul',
PersonalDetails: [ { Age: 43 }, { Email: 'paul@gmail.com' }, { City: 'Texas' } ],
AcademicRecord: [ { Qualification: 'PHD' }, { CGPA: 4 } ]
}
]
Let us dive into the query example to display the records from a collection “Test” to display only the record with its PersonalDetails field size equal to 3 i.e., have 3 fields within it. For this, try the same find() function followed by the field name “PersonalDetails”, “$size” operator, and the pretty() function to display output in an array. The last record that we have just added has been displayed on the console as its “PersonalDetails” array contains 3 field records in it.
[
{
_id: ObjectId("63b38d59a7ac5549359eba34"),
Name: 'Paul',
PersonalDetails: [ { Age: 43 }, { Email: 'paul@gmail.com' }, { City: 'Texas' } ],
AcademicRecord: [ { Qualification: 'PHD' }, { CGPA: 4 } ]
}
]
Conclusion
After going through this guide along with three of the mentioned code examples, you will be able to search for the specific array field from a specific data record containing only the set number of elements via the size operator of MongoDB. We have discussed how a multiple-field array can be utilized in the record to make the size operator work using the find() function instruction. In these illustrations, the size field works to get all those records displayed that have a specific field with a specific number of records in it.