Skip to content

Instantly share code, notes, and snippets.

@OmarMakled
Created April 11, 2017 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OmarMakled/21c1b6aed52b00088d34b091e2a0210c to your computer and use it in GitHub Desktop.
Save OmarMakled/21c1b6aed52b00088d34b091e2a0210c to your computer and use it in GitHub Desktop.
Mongodb Cheat Sheet

Mongodb Cheat Sheet

Insert

Insert one

db.users.insert(
    {_id: 1, name: 'alex'}
)

Insert many

db.users.insert(
    [
        {_id: 2, name: 'jeff'},
        {_id: 3, name: 'bob'}
    ]
)

Insert unordered

db.users.insert(
    [
        {_id: 2, name: 'jeff'},
        {_id: 3, name: 'bob'},
        {_id: 4, name: 'john'}
    ],
    {ordered: false}
)

Update

Update one

db.users.update(
    {_id: 1},
    {
        $set: {
            name: 'Alex',
            age: 20,
            roles: ["admin"]
        }
    }
)

Update push element

db.users.update(
    {_id: 1},
    {
        $push: {
            roles: 'member'
        }
    }
)

Update push each

db.users.update(
    {_id: 1},
    {
        $push: {
            roles: { $each: ['super_admin', 'ceo']}
        }
    }
)

Update all

db.users.update(
    {},
    {$currentDate: { lastModified: true }},
    {multi: true}
)

Replace

Replace one

db.users.replaceOne(
    {_id: 1},
    {author: 'Alex'}
)

Replace or Insert if not exists

db.users.replaceOne(
    {_id: 10},
    {author: 'Alex'},
    {upsert: true}
)

Find

Insert some dummy data

db.inventory.insert( [
    { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
    { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
    { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
    { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
    { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
    { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
    { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
    { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
    { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
    { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
]);

Where qty > 5

db.inventory.find(
    {"qty": {$gt: 5}}
)

And Where qty > 5 && status == "A"

db.inventory.find(
    {
        "qty": {$gt: 20},
        "status": "A"
    }
)

And Where (qty >= 20 && qty <= 50) && status == "A"

db.inventory.find(
    {
        "qty": { $gte: 20, $lte: 50 },
        "status": "A"
    }
)

Or where qty == 100 || status == "D"

db.inventory.find(
    {
        $or: [
            {"qty": 100},
            {"status": "D"}
        ]
    }
)

Or where (qty >= 50 && qty <= 100) || status == "P"

db.inventory.find(
    {
        $or: [
            {"qty": { $gte: 50, $lte: 100 }},
            {"status": "P"}
        ]
    }
)

In

db.inventory.find(
    {
        "status": {$in: ["A", "P"]}
    }
)

And In

db.inventory.find(
    {
        $and: [
            {"status": {$in: ["A", "P"]}},
            {"qty": 100}
        ]
    }
)

Aggregate

Sum distinct

db.inventory.aggregate( [
    {
        $group : {
            _id : "$status",
            total_qty: {$sum: "$qty"}
        }
    }
] )

Sum where equal

db.inventory.aggregate( [
    {
        $group : {
            _id : {
                $eq: ["$status", "A"]
            },
            total_qty: {$sum: "$qty"}
        }
    }
] )

Sum where in

db.inventory.aggregate( [
    {
        $group : {
            _id : {
                $in: ["$status", ["D", "P"]]
            },
            total_qty: {$sum: "$qty"}
        }
    }
] )

Sum where in && Push

db.inventory.aggregate( [
    {
        $group : {
            _id : {
                $in: ["$status", ["D", "P"]]
            },
            total_qty: {$sum: "$qty"},
            more: {$push: {qty: "$qty", item: "$item", status: "$status"}}
        }
    }
] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment