SQL Terms/Concepts | MongoDB Terms/Concepts |
database | database |
table | collection |
row | document |
column | field |
index | index |
table joins | embedded documents and linking |
Show All Databases
1 |
show dbs |
Show Current Database
1 |
db |
Create Or Switch Database
1 |
use user |
Display help on DB methods
1 |
db.help() |
Drop Database
1 |
db.dropDatabase() |
Create Collection
1 |
db.createCollection('user') |
Show Collections
1 |
show collections |
Display help on Collection
1 |
db.collection.help(); |
Drop Collection
1 |
db.user.drop() |
Insert Row
1 2 3 4 5 |
db.user.insertOne({ name: 'koray', surname: 'peker', yearOfBirth: 1987 }) |
Insert Multiple Rows
1 2 3 4 5 6 7 8 9 10 11 |
db.user.insertMany([ { name: 'koray', surname: 'peker', yearOfBirth: 1987 }, { name: 'koray2', surname: 'peker', yearOfBirth: 1992 }, { name: 'koray3', surname: 'peker', yearOfBirth: 2021 } ]) |
Get All Rows
1 |
db.user.find() |
Get All Rows Formatted
1 |
db.user.find().pretty() |
Find Rows
1 |
db.user.find({name : "koray"}) |
Sort Rows
1 2 |
# asc db.user.find().sort({yearOfBirth:1}) |
1 2 |
# desc db.user.find().sort({yearOfBirth:-1}) |
Count Rows
1 2 |
db.user.find().count() db.user.find({name:"koray"}).count() |
Limit Rows
1 |
db.user.find().limit(2) |
Chaining
1 |
db.user.find().limit(2).sort({ yearOfBirth: 1 }) |
Foreach
1 2 3 |
db.user.find().forEach(function(doc) { print("User Name: " + doc.name) }) |
Find One Row
1 |
db.user.findOne({name:"koray"}) |
Find Specific Fields
1 2 |
db.user.find({}, { yearOfBirth: 1, surname: 1 }) db.user.find({"name":"koray"}, {name:true, surname:true, _id:false}) |
Find With Comparison Operators
1 2 3 4 5 6 7 8 |
db.user.find({yearOfBirth: {$eq: 1987}}) db.user.find({yearOfBirth: {$lt: 1987}}) db.user.find({yearOfBirth: {$lte: 1987}}) db.user.find({yearOfBirth: {$gt: 1987}}) db.user.find({yearOfBirth: {$gte: 1987}}) db.user.find({yearOfBirth: {$ne: 1987}}) db.user.find({yearOfBirth: {$in: [1987, 1988]}}) db.user.find({yearOfBirth: {$nin: [1987, 1988]}}) |
Find With Logical Operators
1 2 3 4 |
db.user.find( { $or: [{yearOfBirth: {$lte: 1987}}, {yearOfBirth: {$eq: 1988}}]}) db.user.find( { $and: [{yearOfBirth: {$eq: 1987}}, {name: {$eq: "koray"}}]}) db.user.find( {$not: {yearOfBirth: {$eq: 1992} }}) db.user.find( { $nor: [{yearOfBirth: {$lte: 1987}}, {yearOfBirth: {$eq: 1988}}]}) |
Find With Element Operators
1 |
db.user.find( {name: {$exists: true }}) |
Update Row
1 2 3 4 5 6 7 8 9 10 11 |
db.user.update({ name: "koray" }, { name "koray", surname: "peker", yearOfBirth:1988 }, { upsert: true }) db.user.updateOne({ name: "koray" }, { $set: { yearOfBirth: 1987 } }) |
Update Multiple Rows
1 |
db.user.updateMany({}, { $set: { yearOfBirth: 1987 } }) |
Update Specific Field
1 2 3 4 5 6 |
db.user.update({ name: "koray" }, { $set: { yearOfBirth:1988 } }) |
Increment Field ($inc)
1 2 3 4 5 6 |
db.posts.update({ name: "koray" }, { $inc: { yearOfBirth: 5 } }) |
Rename Field
1 2 3 4 5 6 |
db.posts.update({ name: "koray" }, { $rename: { yearOfBirth: "age" } }) |
Delete Row
1 2 3 |
db.user.remove({ name: "koray" }) db.user.deleteOne({ name: "koray" }) db.user.deleteMany({ yearOfBirth: { $gt: 2000 } }) |
Create An Index
1 |
db.user.createIndex({ name: 1 }) |
Create a Unique Index
1 |
db.user.createIndex( {name:1},{unique:true} ) |
Create a Index On Multiple Fields (Compound Index)
1 |
db.user.createIndex({name:1, surname:-1}) |
Show All Indexes In a Collection
1 |
db.user.getIndexes() |
Drop an Index
1 |
db.user.dropIndex({name:-1}) |
Show Index Statistics
1 |
db.user.stats() |
Mongodb cluster kurulumunda config tanımlanırken cluster nodelarını config file a direk yazabilirmiyiz.
Ve sonrasınsa sistem reboot olsabile doğrudan rs.initiate() olmasını sağlayabilirmiyiz?
Teşekkür ederim