MongoDB Database
MongoDB Database is an open source NoSQL database application. MongoDB Inc. Developed by C++ programming language in 2009 by MongoDB, it is a document-based and scalable application. MongoDB stores data in BSON-based documents, which is a JSON-like data format. MongoDB is a database that is more preferred especially in structures that require speed and traditional relational databases cannot keep up and become cumbersome.
MongoDB Connection Code
To connect with MongoDB Database, it is necessary to run the MongoDB server first. For this, it is sufficient to run the mongod
command in the terminal. Then you can use the mongo
command to access the MongoDB database. This command will open the MongoDB command line interface (CLI). Here you can perform database operations.
MongoDB Select Operation
In MongoDB, data is stored in tables called collections. You can use the db.collection_name.find()
command to view all documents in a collection. For example, the db.users.find()
command lists all users in the users collection. If you want to select documents based on certain criteria, you can send a query object to the find() method. For example, the db.users.find({name: "Ali"})
command returns users whose name field is Ali.
MongoDB Insert Operation
You can use the db.collection_name.insert()
command to add a new document in MongoDB. You need to give the JSON format representation of the document you want to add to this command as a parameter. For example, db.users.insert({name: "Veli", age: 25})
command adds a document with name field Veli and age field 25 to the users collection.
MongoDB Update Process
You can use the db.collection_name.update()
command to update an existing document in MongoDB. You need to give this command the query object of the document you want to update and an update object containing the new values as a parameter. For example, db.users.update({name: "Parent"}, {$set: {age: 26}})
command updates the age field of the document whose name field is Parent to 26.
MongoDB Delete Operation
You can use the db.collection_name.remove()
command to delete an existing document in MongoDB. You need to give the query object of the document you want to delete as a parameter to this command. For example, db.users.remove({name: "Parent"})
command deletes the document whose name field is Parent.
MongoDB Database Code Examples
Below you can find some code examples related to MongoDB.
// Selecting or creating the databaseuse test
// Create a collectiondb.createCollection("products")
// Looking at collectionsshow collections
// Adding a documentdb.products.insert({name: "Laptop", price: 5000, stock: 10})
db.products.insert({name: "Mouse", price: 50, stock: 100})
db.products.insert({name: "Keyboard", price: 100, stock: 50})
// View documentsdb.products.find()
// View a specific documentdb.products.find({name: "Laptop"})
// Document updatedb.products.update({name: "Laptop"}, {$set: {price: 4500}})
// Delete documentdb.products.remove({name: "Mouse"})