Giter VIP home page Giter VIP logo

mongo-replica-sample's Introduction

MongoDB Sample

Data Types

- ObjectId     - Date
- String       - Boolean
- Integer      - Null
- Double       - Array
- NumberLong

Insert, Save

$ db.collection.insert({name: "Yo"})
var name = {name: "Yea"}
$ db.collection.save(name)

Find

$ db.collection.find().pretty()

Update, $set, upsert, multi

$ db.collection.update({name: "a"}, {name: "updated"}) // replace
$ db.collection.update({name: "a", {$set:{name: "Yo"}}}) // field name only
$ db.users.update({name: "d"},{$set:{name: "d", age: 4}}, {upsert: true}) // ถ้าไม่เจอข้อมูลให้ insert
$ db.collection.update({role: "normal"}, {skill: "js"}, {multi: true}) // multi update

Remove

$ db.collection.remove({name: "a"}) // Maths
$ db.collection.remove({name: "a"}, 1) // ล่าสุด 1 อัน
$ db.collection.remove({}) // all

Operator

- $eq, $ne
- $in, $nin (Array)
- $gt,$lt, $gte, $lte
- $exists := true, false

Aggregate $group, $sum, $avg, $min, $max, $first, $last

$ db.users.aggregate([{$group:{_id: "$role", total_role:{$sum:1}}}])

Distinct

$ db.users.distinct("role")

Count

$ db.users.count()
$ db.users.find({age: {$lte: 2}}).count()

Sorting

$ db.users.find().sort({name: 1}) // 1 asc
$ db.users.find().sort({age: -1}) // -1 desc
$ db.users.find().sort({name: 1, age: -1})

Data Modeling

  • References
  • Embedded Data

User/Role

Sorting $natural

$ db.users.find().sort({$natural: 1})

Indexes createIndex, getIndexes, dropIndex

$ db.users.createIndex({name: 1})
$ db.users.getIndexes()
$ db.users.dropIndex({name: 1})

Explain

$ db.users.find({name: "a"}).explain()
$ db.users.find({name: "a"}).explain("queryPlanner")
$ db.users.find({name: "a"}).explain("executionStats")
$ db.users.find({name: "a"}).explain("allPlansExecution")
$ db.users.find({name: "a"}).explain(true)
$ db.users.find({name: "a"}).explain(false)

Group by user Reduce Function

$ db.users.group({key: {role: 1}, reduce: function(a, result){ result.sum_age += a.age}, initial:{sum_age: 0}})

Regular Expression in MongoDB $regex

$ db.users.find({name: {$regex: /^a/}})
$ db.users.find({name: /a/})

Limit

$ db.users.find().limit(1)

Map-Reduce in MongoDB

mongo-replica-sample's People

Contributors

yuttasakcom avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.