 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Limit fields and slice projection together?
Use the $slice operator. Let us first create a collection with documents −
> db.limitAndSliceProjectionDemo.insertOne(    {       "_id" : 101,       "UserName" : "Carol",       "UserAge" : 26,       "UserMesssage" : [          "Hi",          "Hello",          "Bye",          "Awesome",          "Good",          "Bad",          "Nice",          "Good Night",          "Good Morning"       ]    } ); { "acknowledged" : true, "insertedId" : 101 } Following is the query to display all documents from a collection with the help of find() method −
> db.limitAndSliceProjectionDemo.find().pretty();
This will produce the following output −
{    "_id" : 101,    "UserName" : "Carol",    "UserAge" : 26,    "UserMesssage" : [       "Hi",       "Hello",       "Bye",       "Awesome",       "Good",       "Bad",       "Nice",       "Good Night",       "Good Morning"    ] } Following is the query to limit fields and perform slice projection together. Here, we are slicing from 2 to 4 −
> db.limitAndSliceProjectionDemo.find({ "UserName" : "Carol" }, {"_id": 0, "UserName":0,"UserAge":0, "UserMesssage": { "$slice": [2,4] } }).pretty(); This will produce the following output −
{ "UserMesssage" : [ "Bye", "Awesome", "Good", "Bad" ] }Advertisements
 