- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to limit the amount of characters returned from a field in a MongoDB?
To limit the amount of characters returned from a field, use $substr in MongoDB. Let us create a collection with documents −
> db.demo233.insertOne({"Paragraph":"My Name is John Smith.I am learning MongoDB database"}); { "acknowledged" : true, "insertedId" : ObjectId("5e41877df4cebbeaebec5146") } > db.demo233.insertOne({"Paragraph":"David Miller is a good student and learning Spring and Hibernate Framework."}); { "acknowledged" : true, "insertedId" : ObjectId("5e4187d7f4cebbeaebec5147") }
Display all documents from a collection with the help of find() method −
> db.demo233.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e41877df4cebbeaebec5146"), "Paragraph" : "My Name is John Smith.I am learning MongoDB database" } { "_id" : ObjectId("5e4187d7f4cebbeaebec5147"), "Paragraph" : "David Miller is a good student and learning Spring and Hibernate Framework." }
Following is the query to limit the amount of characters returned from a field in MongoDB −
> db.demo233.aggregate( ... [ ... { ... $project: ... { ... Paragraph: { $substr: [ "$Paragraph", 0, 10] } ... ... } ...} ] )
This will produce the following output −
{ "_id" : ObjectId("5e41877df4cebbeaebec5146"), "Paragraph" : "My Name is" } { "_id" : ObjectId("5e4187d7f4cebbeaebec5147"), "Paragraph" : "David Mill" }
- Related Articles
- How to limit the amount of characters returned from a field in a MongoDB query?
- How to limit the number of results returned from grep in Linux?
- MongoDB query to limit the returning values of a field?
- Limit number of values in a field using MongoDB?
- Limit number of values in a field with MongoDB?
- How to limit the number of characters allowed in form input text field?
- How to give a limit to the input field in HTML?
- How to remove special characters from a database field in MySQL?
- How to query MongoDB with a LIMIT?
- How to remove a field completely from a MongoDB document?
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- How to limit the number of characters entered in a textarea in an HTML form?
- How to change the type of a field in MongoDB?
- How can we limit the number of characters inside a JTextField in Java?
- How to increment a field in MongoDB?

Advertisements