0
0
MongoDBquery~5 mins

String and number types in MongoDB

Choose your learning style9 modes available
Introduction

Strings and numbers store text and numeric values in a database. They help keep data organized and easy to use.

Storing a person's name or address as text.
Saving a product price or quantity as a number.
Recording dates or times as strings or numbers.
Keeping phone numbers or IDs as strings to preserve formatting.
Syntax
MongoDB
{ fieldName: "string value" } or { fieldName: 123 }

Strings are enclosed in double quotes.

Numbers can be integers or decimals without quotes.

Examples
This stores the name as a string.
MongoDB
{ name: "Alice" }
This stores the age as a number.
MongoDB
{ age: 30 }
This stores a decimal number for price.
MongoDB
{ price: 19.99 }
Phone numbers are stored as strings to keep dashes.
MongoDB
{ phone: "123-456-7890" }
Sample Program

This adds a product with a string name, a decimal price, and a number stock count. Then it shows all products.

MongoDB
db.products.insertOne({ name: "Notebook", price: 5.99, stock: 100 })
db.products.find({})
OutputSuccess
Important Notes

Strings can hold letters, numbers, and symbols.

Numbers are used for math operations in queries.

Always use quotes for strings, no quotes for numbers.

Summary

Strings store text inside quotes.

Numbers store numeric values without quotes.

Use the right type to keep data clear and useful.