MongoDB Indexing Search validation
Indexing & Search
MongoDB Atlas comes with a full-text search engine that can be used to search for documents in a collection.
Atlas Search is powered by Apache Lucene.
Creating an Index
We'll use the Atlas dashboard to create an index on the "sample_mflix" database from the sample data that we loaded in the Intro to Aggregations section. You first should login to Atlas dashboard by mongodb.com and sign in.
- From the Atlas dashboard, click on your Cluster name then the Search tab.
- Click on Go to Atlas Search button
- Click on the Create Search Index button.
- Use the Visual Editor and click Next.
- Name your index, choose the Database and Collection you want to index and click Next.
- If you name your index (e.g. mongoIndex) if you take “default” you will not have to specify the index name in the
$search
pipeline stage. - Choose the
sample_mflix
database and themovies
collection.
- If you name your index (e.g. mongoIndex) if you take “default” you will not have to specify the index name in the
- Click Create Search Index and wait for the index to complete.
- then you have mongoIndex and Overview and other info and you can see the following image:
Running a Query
To use our search index, we will use the $search
operator in our aggregation pipeline.
Example
db.movies.aggregate([ { $search: { index: "default", // optional unless you named your index something other than "default" text: { query: "star wars", path: "title" }, }, }, { $project: { title: 1, year: 1, } } ])
The first stage of this aggregation pipeline will return all documents in the movies
collection that contain the word “star” or “wars” in the title
field.
The second stage will project the title
and year
fields from each document
Now you can login Mongosh in the command prompt as described in my preview post: MongoDB Getting Started
Go to Sample_mflex database by “use Sample_mflex” then you are in this database and run the above query:
Schema Validation
By default MongoDB has a flexible schema. This means that there is no strict schema validation set up initially.
Schema validation rules can be created in order to ensure that all documents a collection share a similar structure.
MongoDB supports JSON Schema validation. The $jsonSchema
operator allows us to define our document structure.
Example:
db.createCollection("posts", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "title", "body" ],
properties: {
title: {
bsonType: "string",
description: "Title of post - Required."
},
body: {
bsonType: "string",
description: "Body of post - Required."
},
category: {
bsonType: "string",
description: "Category of post - Optional."
},
likes: {
bsonType: "int",
description: "Post like count. Must be an integer - Optional."
},
tags: {
bsonType: ["string"],
description: "Must be an array of strings - Optional."
},
date: {
bsonType: "date",
description: "Must be a date - Optional."
}
}
}
}
})
This will create the posts
collection in the current database and specify the JSON Schema validation requirements for the collection.
If you run the above in the mongosh you shall return {ok, 1 }
More look to the Schema Validation
MongoDB Data API
The MongoDB Data API can be used to query and update data in a MongoDB database without the need for language specific drivers.
Language drivers should be used when possible, but the MongoDB Data API comes in handy when drivers are not available or drivers are overkill for the application.
Read & Write with the MongoDB Data API
The MongoDB Data API is a pre-configured set of HTTPS endpoints that can be used to read and write data to a MongoDB Atlas database.
With the MongoDB Data API, you can create, read, update, delete, or aggregate documents in a MongoDB Atlas database.
Cluster Configuration
In order to use the Data API, you must first enable the functionality from the Atlas UI.
From the MongoDB Atlas dashboard, navigate to Data API in the left menu.
Select the data source(s) you would like to enable the API on and click Enable the Data API. As you see in the above image Data API is enabled.
Access Level
By default, no access is granted. Select the access level you’d like to grant the Data API. The choices are: No Access, Read Only, Read and Write, or Custom Access. You can do that in the above figure at the dropdown under Data API Access.
Data API Key
In order to authenticate with the Data API, you must first create a Data API key. press to the user then you can see Create API key in the upper right side.
Click Create API Key, enter a name for the key, then click Generate API Key. as seen in the following figure:
Be sure to copy the API key and save it somewhere safe. You will not get another chance to see this key again.
Sending a Data API Request
We can now use the Data API to send a request to the database.
In the next example, we’ll use curl to find the first document in the movies
collection of our sample_mflix
database. We loaded this sample data before.
To run this example, you’ll need your App Id, API Key, and Cluster name.
You can find your App Id in the URL Endpoint field of the Data API page in the MongoDB Atlas UI.
Example
curl --location --request POST 'https://data.mongodb-api.com/app/<DATA API APP ID>/endpoint/data/v1/action/findOne' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <DATA API KEY>' \
--data-raw '{
"dataSource":"<CLUSTER NAME>",
"database":"sample_mflix",
"collection":"movies",
"projection": {"title": 1}
}'
Data API Endpoints
In the previous example, we used the findOne
endpoint in our URL.
There are several endpoints available for use with the Data API.
All endpoints start with the Base URL: https://data.mongodb-api.com/app/<Data API App ID>/endpoint/data/v1/action/
Find a Single Document
Endpoint
POST Base_URL/findOne
The findOne
endpoint is used to find a single document in a collection.
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "filter": <query filter>, "projection": <projection> }
Find Multiple Documents
Endpoint
POST Base_URL/find
The find
endpoint is used to find multiple documents in a collection.
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "filter": <query filter>, "projection": <projection>, "sort": <sort expression>, "limit": <number>, "skip": <number> }
Insert a Single Document
Endpoint
POST Base_URL/insertOne
The insertOne
endpoint is used to insert a single document into a collection.
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "document": <document> }
Insert Multiple Documents
Endpoint
POST Base_URL/insertMany
The insertMany
endpoint is used to insert multiple documents into a collection.
Request Body
Example
{
"dataSource": "<data source name>",
"database": "<database name>",
"collection": "<collection name>",
"documents": [<document>, <document>, ...]
}
Update a Single Document
Endpoint
POST Base_URL/updateOne
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "filter": <query filter>, "update": <update expression>, "upsert": true|false }
Update Multiple Documents
Endpoint
POST Base_URL/updateMany
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "filter": <query filter>, "update": <update expression>, "upsert": true|false }
Delete a Single Document
Endpoint
POST Base_URL/deleteOne
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "filter": <query filter> }
Delete Multiple Documents
Endpoint
POST Base_URL/deleteMany
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "filter": <query filter> }
Aggregate Documents
Endpoint
POST Base_URL/aggregate
Request Body
Example
{ "dataSource": "<data source name>", "database": "<database name>", "collection": "<collection name>", "pipeline": [<pipeline expression>, ...] }
Conclusion
In this post I have explored MongoDB Indexing, Search, Validation, Data API, how to use to send data to data to Database by using curl. Described Endpoind, Data API Key, Find, Insert, update, delete and aggeraget operations.
In my next post I will explore MongoDB Drivers
This post is part of MongoDB-Step by step