MongoDB Tutorial Deutsch für Anfänger. Mit MongoDB als NoSQL Datenbank kannst du deine Daten direkt als Objekt im JSON Format persistieren und auch so wieder auslesen. Ich zeige dir was du alles brauchst und stelle dir alle wichtigen Befehle in der MongoDB Shell vor. Happy Coding!
Tutorial auf YouTube anschauen
show dbs
db
use tutorial
db.dropDatabase('tutorial')
db.createCollection('products')
show collections
db.products.drop()
db.products.insertOne( { name: "Banane", price: 0.99, category: 'Obst', views: 16 } )
db.products.insertMany( [ { name: "Apfel", price: 0.60, category: 'Obst', views: 2 }, { name: "Apfel Neu", price: 0.80, category: 'Obst', views: 10 }, { name: "Kiwi", price: 1.19, category: 'Obst', views: 0, ratings: [ { user: 'Paul', stars: 4}, { user: 'Tom', stars: 5}, { user: 'Max', stars: 3} ] } ] )
db.products.find()
db.products.find().pretty()
db.products.find({ name: 'Apfel' })
db.products.find().sort({ name: 1 }) db.products.find().sort({ name: -1 })
db.products.find().count() db.products.countDocuments()
db.products.find().limit(2)
db.products.find().limit(2).sort({ price: 1 })
db.products.find().forEach(function(doc) { print("Produkt: " + doc.name) })
db.products.find({ price: { $gt: 1 } }) db.products.find({ price: { $gte: 0.99 } }) db.products.find({ price: { $lt: 0.99 } }) db.products.find({ price: { $lte: 0.99 } })
db.products.createIndex( { name: 'text' }, { default_language: "german" } )
db.products.getIndexes()
db.products.dropIndex('name_text')
db.products.find({ $text: { $search: "Apfel" } })
db.products.findOne({ category: 'Obst' })
db.products.find({ category: 'Obst' }, { name: 1, price: 1 })
db.products.find({}, { name: 1, price: 1 })
db.products.find({}, { ratings: 0, date: 0 })
db.products.updateOne({ name: "Banane" }, { $set: { price: 1.29 } })
db.products.updateOne({ name: 'Gurke' }, { $set: { price: 0.5 , category: 'Gemüse', views: 0 } }, { upsert: true })
db.products.updateMany({ category: "Obst" }, { $set: { price: 0.2 } })
db.products.updateOne({ name: 'Gurke' }, { $inc: { views: 1 } })
db.products.update({ name: 'Gurke' }, { $rename: { views: 'likes' } })
db.products.aggregate([ { $match: { price: { $lt: 0.99 } } }, { $group: { _id: "$category", total_views: { $sum: "$views"} } } ])
db.products.deleteOne({ name: 'Gurke' })
db.products.deleteMany({ category: 'Obst' })