Page Content

Tutorials

How do I exclude a field in MongoDB Projection?

MongoDB Projections

MongoDB relies on Projections, Limit, and Sort to efficiently get and display data. They give you complete control over the type, quantity, and sequence of data that is returned. In addition to being offered as strong stages within the aggregate framework, these operations can be performed directly to find() methods.

Projections

Instead of obtaining the complete document, projections allow you to extract just the information that is required. As a result, less data is transmitted across the network, and the client-side processing needs are reduced. The projection document is a second optional parameter that the find() method takes in order to determine which fields to include or exclude.

  • Including fields: In order to display particular fields, you must set their value in the projection document to 1 (or true).
    • The _id field: Even if it isn’t stated specifically, the _id field is automatically included in the results.
  • Excluding fields: You can make certain fields invisible by setting their value to 0 (or false).
    • Excluding _id: Setting _id to 0 explicitly is required if you want to exclude the _id field.
  • Mixed Projections: In the same projection document, you cannot combine inclusion (1) and exclusion (0) parameters, with the exception of the _id field. You have to either indicate the fields you wish to keep or the fields you wish to keep out.

Example of Projection: Consider a collection mycol with documents containing _id, title, and by fields.

To display only the title field:

> db.mycol.find({}, {"title": 1})

To display only the title field and exclude the _id field:

> db.mycol.find({}, {"title": 1, "_id": 0})
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}

To display all fields except the by field:

> db.mycol.find({}, {"by": 0})

Projections can also be used with array fields using operators like $slice. This allows you to return a subset of elements from an array within a document.

Example of $slice Projection for an Array: To return only the first 2 elements of a ratings array:

db.inventory.find( { _id: 5 }, { ratings: { $slice: 2 } } )

To return the last 10 comments from a comments array:

db.blog.posts.findOne(criteria, {"comments" : {"$slice" : -10}}) 

To skip the first 23 elements and return the next 10:

db.blog.posts.findOne(criteria, {"comments" : {"$slice" : }}) 

limit() method The amount of documents that can be returned in a query’s result set can be limited using MongoDB’s limit() method. One number type input, which indicates the maximum number of documents to be shown, is accepted.

Syntax of limit() method:

>db.COLLECTION_NAME.find().limit(NUMBER)

Limit() will show every document in the collection if you don’t supply the number argument. Limit() reduces the amount of the dataset to be sorted, which helps prevent performance problems when sorting on non-indexed fields when used in conjunction with sort().

Example of limit(): To display only two documents from mycol collection:

> db.mycol.find().limit(2)

Or, when combined with projection:

> db.mycol.find({},{"title":1,_id:0}).limit(2) 
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}

sort() method To arrange the documents in a query’s result set, use the sort() method. A document with one or more fields specified and their sorting order is accepted.

  • Sorting Order: The sorting order is -1 for descending order and 1 for ascending order.
  • Default Behaviour: sort() will automatically display the pages in ascending order if you don’t indicate a sorting option.
  • Multiple Fields: The results will be sorted in the order specified if more than one key is supplied.

Syntax of sort() method: >db.COLLECTION_NAME.find().sort({KEY:1})

Example of sort(): To display documents sorted by title in descending order:

> db.mycol.find({},{"title":1,_id:0}).sort({"title":-1}) 
{"title":"Tutorials Point Overview"}
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}

To sort by username ascending and age descending:

> db.c.find().sort({username : 1, age : -1})

Additionally, MongoDB specifies a particular comparison order for certain data types throughout the sorting process. Null values, for example, appear before numbers, numbers before strings, and so forth.

Find() MongoDB The find() method is the main mechanism to query and retrieve MongoDB documents. Without parameters, db.COLLECTION_NAME.find() displays all documents in that collection. Find() accepts a query document to match documents, analogous to a SQL WHERE clause. Example: db.mycol.find(“by”:”tutorials point”) returns documents with “by” field equal to “tutorials point”. Chaining the.pretty() method formats output, especially in mongo shell, to make it more readable. Use the cursor object from find() to iterate over the results. Any cursor without a variable in the mongo shell iterates and outputs the first 20 documents, asking Type to see more.

Syntax:

db.collection.find(query, projection)

Code Example:

Find all products:

db.products.find({});

Find products with price greater than 100:

db.products.find({ "price": { "$gt": 100 } });

Find products in the ‘Electronics’ category and project only ‘name’ and ‘price’:

db.products.find({ "category": "Electronics" }, { "name": 1, "price": 1, "_id": 0 });

MongoDB FindAndModify() The findAndModify() command finds, modifies, and returns a document atomically. It ensures no other processes interfere with the find-and-modify process, making it popular in transactional workflows. This command receives query (a document query selection), update (a document indicating changes), and remove (a Boolean to signal document removal). MongoDB 3.2 included shell methods like findOneAndDelete, findOneAndReplace, and findOneAndUpdate to simplify findAndModify.

Syntax:

db.collection.findAndModify({
    query: <query>,
    sort: <sort>,
    update: <update>,
    fields: <projection>,
    upsert: <boolean>,
    new: <boolean>,
    remove: <boolean>
});

Code Example:

db.products.findAndModify({
    query: { "inStock": true },
    update: { "$set": { "inStock": false } },
    new: false // Return the original document before update
});

FindOne() MongoDB Seems like find(), but return only one document. If numerous documents meet the query criteria, findOne() returns the first. Use with an empty object ({}) to retrieve and return the first document in the collection. FindOne() works like find() + limit(1). FindOne() is recommended for single results.

Syntax:

db.collection.findOne(query, projection)

Code Example:

Find a product by its _id:

db.products.findOne({ "_id": 2 });

Find any product with price less than 50:

db.products.findOne({ "price": { "$lt": 50 } });

MongoDB findOneAndDelete() This function was added to MongoDB 3.2.x’s new CRUD API to clarify operation semantics. Only one document that matches the filter is destroyed and returned. A filter document is its argument.

Syntax:

db.collection.findOneAndDelete(filter, options)

Code Example:

Find and delete a product that is out of stock, returning the deleted document:

db.products.findOneAndDelete({ "inStock": false });

db.collection.findOneAndReplace() Method The new CRUD API in MongoDB 3.2.x added another method. findOneAndReplace() atomically replaces a document that matches a filter. First, it accepts the filter, then the replacement document. This method returns the document before or after replacement, based on returnNewDocument.

Syntax:

db.collection.findOneAndReplace(filter, replacement, options)

Code Example:

Find the “Keyboard” and replace it with a new document, returning the new document:

db.products.findOneAndReplace(
    { "name": "Keyboard" },
    { "name": "Ergonomic Keyboard", "category": "Electronics", "price": 90, "inStock": true, "brand": "Logitech" },
    { returnDocument: "after" } // Return the document after replacement
);

MongoDB db.collection.findOneAndUpdate() In MongoDB 3.2.x+, findOneAndUpdate() atomically changes a single document that meets the filter and returns it before or after the update. It takes two parameters: a filter document and a modifier document explaining the changes. This function was modified in MongoDB 4.2 to accept an aggregation pipeline for the update, which can include stages like $addFields, $project, and $replaceRoot.

Syntax:

db.collection.findOneAndUpdate(filter, update, options)

Code Example:

Find the “Laptop”, increase its price by 50, and return the updated document:

db.products.findOneAndUpdate(
    { "name": "Laptop" },
    { "$inc": { "price": 50 } },
    { returnDocument: "after" } // Return the document after update
);

CopyTo() in MongoDB A copyTo() method for general MongoDB collection operations. DB.copyDatabase() is a database-copying command. Copying a database or collection between instances is easy with the copyDatabase command.

Syntax:

db.collection.copyTo(newCollectionName)

Code Example:

// This method is deprecated and should not be used in modern MongoDB.
// For demonstration purposes only:
db.products.copyTo("products_backup");

Recommended Alternative using Aggregation ($out):

db.products.aggregate([
    { "$match": {} }, // Match all documents
    { "$out": "products_backup" } // Output to new collection
]);

Db.Collection.Count() MongoDB method.count() MongoDB’s count() method returns the number of documents. It can be used without parameters to count all documents in a collection or with a query document to count documents that meet criteria. Counting documents in a collection is rapid regardless of size. But adding criteria to the count can slow things down. This approach helps display “results 0–10 of 439” when paginating.

Syntax:

db.collection.count(query, options)

Code Example:

// This method is deprecated. For demonstration purposes only:
db.products.count({ "category": "Electronics" });

MongoDB countDocuments() The countDocuments() method returns the count of documents that match the specified query criteria. It performs a full scan of the documents that match the query.

Syntax:

db.collection.countDocuments(query, options)

Code Example:

Count all documents in the products collection:

db.products.countDocuments({});

Count products that are in stock:

db.products.countDocuments({ "inStock": true });

Method Distinct() MongoDB Distinct() finds all unique field values in a collection. You must specify the collection and key (field) to obtain separate values. This method is excellent for extracting unique data, such as GridFS filenames, when you need to deal with potential duplication or just care about the field’s unique occurrences.

Syntax:

db.collection.distinct(field, query, options)

Code Example:

Get all distinct categories from the products collection:

db.products.distinct("category");

Combining Limit, Skip and Sort for Pagination

To provide more intricate query functionality, such pagination, these techniques can be combined.

Syntax combining limit() and skip(): >db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

It usually doesn’t matter which order you execute skip(), limit(), and sort() in the mongo shell because the arguments are forwarded to the MongoDB server along with the query for processing.

Example of combined limit(), skip(), and sort(): To display only the second document by sorting, skipping the first, and limiting to one:

> db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1) 
{"title":"NoSQL Overview"}

To get two documents from a descending sorted collection, skipping the first one:

db.test.find({}).sort({ "name" : -1}).skip(1).limit(2) 
{ "_id" : ObjectId("592516d7fbd5b591f53237b3"), "name" : "Sonny", "age" : "28", "status" : "away" }
{ "_id" : ObjectId("592516d7fbd5b591f53237b4"), "name" : "Cher", "age" : "20", "status" : "online" }

Avoiding Large Skips: skip() works well for modest document counts, but it can be slow and ineffective to use large skip values (such as 10,000 or more). In order to return the desired documents, MongoDB must first locate and then discard all of the skipped results. When paginating huge datasets, using a range condition based on the last document returned from the previous page is a more effective method, particularly when sorting by a unique or often growing attribute like a date.

MongoDB’s document-oriented database has many ways for producing, reading, updating, and removing data, as well as aggregating and administrative duties.

Aggregation Framework Context

These query notions ($project, $limit, $sort, and $skip) are also essential pipeline steps in MongoDB’s Aggregation Framework, which is worth noting. Documents are processed by the aggregation pipeline in a sequence of steps, with the output of one step serving as the input for the subsequent one. This makes it possible to perform intricate data analysis and manipulations.

For instance, $match functions similarly to a WHERE clause, $project similarly to a SELECT, $group similarly to GROUP BY, and $sort, $skip, and $limit carry out their standard ordering and quantity control functions. It is excellent practice to use $match early in the pipeline since it can take use of indexes and decrease the volume of documents that later stages process, which will improve performance.

Index