db.articles.insertOne( { subject: "coffee", author: "xyz", views: 50 } )
db.articles.insertMany( [ { subject: "coffee", author: "xyz", views: 50 }, { subject: "Coffee Shopping", author: "efg", views: 5 }, { subject: "Baking a cake", author: "abc", views: 90 }, { subject: "baking", author: "xyz", views: 100 }, { subject: "Café Con Leche", author: "abc", views: 200 }, { subject: "Сырники", author: "jkl", views: 80 }, { subject: "coffee and cream", author: "efg", views: 10 }, { subject: "Cafe con Leche", author: "xyz", views: 10 }, { subject: "coffees", author: "xyz", views: 10 }, { subject: "coffee1", author: "xyz", views: 10 } ] )
db.people.find() - SELECT * FROM people db.people.find({ }, { user_id: 1, status: 1 }) - SELECT _id, user_id, status FROM people db.people.find({ },{ user_id: 1, status: 1, _id: 0 }) - SELECT user_id, status FROM people db.people.find({ status: "A" }) - SELECT * FROM people WHERE status = "A" db.people.find({ status: "A", age: 50 }) - SELECT * FROM people WHERE status = "A" AND age = 50 db.people.find({ $or: [ { status: "A" } , { age: 50 } ] }) - SELECT * FROM people WHERE status = "A" OR age = 50
$eq = Matches values that are equal to a specified value. $gt > Matches values that are greater than a specified value. $gte >= Matches values that are greater than or equal to a specified value. $in Matches any of the values specified in an array. $lt < Matches values that are less than a specified value. $lte <= Matches values that are less than or equal to a specified value. $ne != Matches all values that are not equal to a specified value. $nin Matches none of the values specified in an array.
db.people.find({ age: { $gt: 25 } }) - SELECT * FROM people WHERE age > 25 db.people.find({ age: { $lt: 25 } }) - SELECT * FROM people WHERE age < 25 db.people.find({ age: { $gt: 25, $lte: 50 } }) - SELECT * FROM people WHERE age > 25 AND age <= 50 db.people.find( { user_id: /bc/ } ) db.people.find( { user_id: { $regex: /bc/ } } ) - SELECT * FROM people WHERE user_id like "%bc%" db.people.find( { user_id: /^bc/ } ) db.people.find( { user_id: { $regex: /^bc/ } } ) - SELECT * FROM people WHERE user_id like "bc%" db.people.find( { status: "A" } ).sort( { user_id: 1 } ) - SELECT * FROM people WHERE status = "A" ORDER BY user_id ASC db.people.find( { status: "A" } ).sort( { user_id: -1 } ) - SELECT * FROM people WHERE status = "A" ORDER BY user_id DESC db.people.count() db.people.find().count() - SELECT COUNT(*) FROM people db.people.count( { user_id: { $exists: true } } ) db.people.find( { user_id: { $exists: true } } ).count() - SELECT COUNT(user_id) FROM people db.people.count( { age: { $gt: 30 } } ) db.people.find( { age: { $gt: 30 } } ).count() - SELECT COUNT(*) FROM people WHERE age > 30 db.people.distinct( "status" ) - SELECT DISTINCT(status) FROM people db.people.findOne() db.people.find().limit(1) - SELECT * FROM people LIMIT 1
- db.people.updateMany( { age: { $gt: 25 } }, { $set: { status: "C" } } ) - SQL 변환하면, - UPDATE people SET status = "C" WHERE age > 25 - 한 Document만 수정하려면 updateOne을 사용함 - db.people.updateOne( { age: { $gt: 25 } }, { $set: { status: "C" } } ) - db.people.updateMany( { status: "A" } , { $inc: { age: 3 } } ) - SQL 변환하면, - UPDATE people SET age = age + 3 WHERE status = "A"