Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB Basic

MongoDB Basic

yuanxin.qiu

August 28, 2013
Tweet

More Decks by yuanxin.qiu

Other Decks in Technology

Transcript

  1. MongoDB CRUD ܱ৳ หׄ ᄎົčࡏܒaࡓ॥aСٺĎ insert db.test.insert({name: "YuanxinQiu", age: 23})

    db.test.insert({name: "Kakaxi", age: 50, sex: 1}) ଏಪ҂ᆦӻ஻ਈҬೆđ҂ݖ۲۱ adapter ׻յ๙ਔ MongoDB ଽ֥҆஻ਈҬೆٚمb find // 查找年龄为 23 的记录 db.users.find({age: 23}) // 查找年龄在 20 ~ 30 的记录 db.users.find({age: {$gt: 20, $lt: 30}}) // 查找年龄为 23 或 50 的记录 db.users.find({$or: [{age: 23}, {age: 50}]}) // 查找年龄⼤大于30 或 年龄⼩小于20 的记录 db.users.find({$or: [{age: {$gt: 30}}, {age: {$lt: 20} }]}) // skip limit db.users.find().skip(1).limit(10) ӈႨ֥ ֩Ⴟ, $gt, $lt, $gte, $lte, $ne ֩Ⴟ, $or, $in, $nin ߎᆦӻᆞᄵіղൔ // ⽤用户名字以 j 开头, e 结尾 db.users.find({name: /^j/, name: /e$/}) ۚࠩҰ࿘
  2. count distinct group mapReduce update ݦඔࢤ१ db.collection.update(criteria, objNew, upsert, mult)

    criteria ླေ۷ྍ֥іղൔ objNew ۷ྍіղൔ upsert ҂թᄝൈđ൞ڎҬೆ mult ൞ڎ۷ྍ؟۱໓֖ // 将年龄为 23 的⼈人,sex 改为 0 db.users.update({age: 23}, {$set: {sex: 0}}) अ҆۷ྍ ଏಪđ۷ྍൈđ൞ᆰࢤࡼ໓ู֖ߐ֥b db.users.insert({name: "YuanxinQiu", age: 23}) // => {"_id": ObjectId("5214ccc35493607a0eae4e0f"), name: "YuanxinQiu", age: 23} db.users.update({age: 23}, {name: "QiuYuanxin"}) // => {ObjectId("5214ccc35493607a0eae4e0f"), name: "QiuYuanxin"} $inc ބ $set $inc ࠧđᄹਈ۷ྍĠط $set ࠧđᆰࢤഡᇂᆴ. db.users.insert({name: "YuanxinQiu", age: 23}) // => {"_id": ObjectId("5214ccc35493607a0eae4e0f"), name: "YuanxinQiu", age: 23} db.users.update({age: 23}, {$inc: {age: 2}}) // => {"_id": ObjectId("5214ccc35493607a0eae4e0f"), name: "YuanxinQiu", age: 25} db.users.update({age: 23}, {$set: {age: 25}}) // => {"_id": ObjectId("5214ccc35493607a0eae4e0f"), name: "YuanxinQiu", age: 25} remove
  3. // 删除某⽤用户 var id = db.users.findOne({name: "Yuanxin.Qiu"})._id db.users.remove(id) // 条件删除,T掉未成年⼈人

    db.users.remove({ age: {$lt: 18} }) // 删除所有数据 db.users.remove() іܱ৳ ⼀一对多 ა 多对多 ׻൞๙ݖ embeded ֥ٚൔൌགྷđᆺ൞഍ັႵ෮҂๝b // One to Many db.companies.insert({name: "⼩小⽶米公司", product: [ {ObjectId: "1", name: "⼩小⽶米"}, {ObjectId: "2", name: "红⽶米", price: 23.34} ]}) // 查找某公司,他有⼀一款产品,名为 “⼩小⽶米” db.companies.find({product: {name: "⼩小⽶米"}}) // Many to Many db.students.insert({name: "⼩小⽶米"}) // => ObjectId("1") db.students.insert({name: "红⽶米"}) // => ObjectId("2") db.teachers.insert({name: "雷军", students: [ObjectId("1"), ObjectId("2")] }) // 查找某个学⽣生,他被哪些⽼老师教 t1 = db.students.findOne({name: "⼩小⽶米"}) db.teachers.find({students: t1._id}) หׄ ૫ཟ collection թԥ ଆൔሱႮ ᆦӻ׮෿Ұ࿘č఼ն֥Ұ࿘ეओĎ
  4. ൐Ⴈؽࣉᇅඔऌթԥ ሱ׮ԩ৘ෞோđၛᆦӻᄉࠩљ֥ഥ෪ྟ ... ᄎົ ᇶՖСٺ ٳோ ڬЧࠢ Mongostat MongoDBСٺٚൔ ሧਘ

    1. ܲٚ໓֖ 2. ϖ฿࿐๙MongoDB ঔᅚᄇ؀ 1. ൪तᇏݓ֥NoSQLᆭਫ਼ğՖMySQL֞MongoDB 2. MySQL vs.MongoDB ۲Ⴕ഻ڵĆ