help:mongodb:refsheet
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | help:mongodb:refsheet [2020/06/20 14:39] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~NOTOC~~ | ||
+ | <WRAP noprint> | ||
+ | |<100% 25% - >| | ||
+ | ^ \\ DATA ANALYTICS REFERENCE DOCUMENT\\ \\ ^^ | ||
+ | ^ Document Title:|The HDIP Data Analytics MongoDB quick reference sheet| | ||
+ | ^ Document No.: | ||
+ | ^ Author(s): | ||
+ | ^ Contributor(s): | ||
+ | |||
+ | |||
+ | **REVISION HISTORY** | ||
+ | |< 100% 10% - - 10% 17% 10% >| | ||
+ | ^ \\ Revision\\ | ||
+ | | [[: | ||
+ | |||
+ | |||
+ | ---- | ||
+ | </ | ||
+ | ====== MongoDB Quickref Sheet ====== | ||
+ | |||
+ | <WRAP group> | ||
+ | |||
+ | <WRAP third column> | ||
+ | **Admin Commands** | ||
+ | <code javascript> | ||
+ | help | ||
+ | db.help() | ||
+ | |||
+ | show dbs | ||
+ | show databases | ||
+ | use <db> | ||
+ | show collections | ||
+ | </ | ||
+ | |||
+ | **aggregate $ keywords** ((https:// | ||
+ | |||
+ | <code mongo> | ||
+ | $group | ||
+ | $avg | ||
+ | $min | ||
+ | $max | ||
+ | $sum | ||
+ | $first | ||
+ | $last | ||
+ | $lookup | ||
+ | $project | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | <WRAP third column> | ||
+ | |||
+ | **DB Commands** | ||
+ | <code javascript> | ||
+ | db.collection.save() | ||
+ | db.collection.find() | ||
+ | db.colle....find().pretty() | ||
+ | db.collection.findOne() | ||
+ | |||
+ | db.collection.insert() | ||
+ | db.collection.update() | ||
+ | </ | ||
+ | |||
+ | **Sorting** ((https:// | ||
+ | <code mongo> | ||
+ | cursor.sort() | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | <WRAP third column> | ||
+ | |||
+ | **find/ | ||
+ | <code javascript> | ||
+ | $and: [{},{}] | ||
+ | $or: [{},{}] | ||
+ | $in: [1,2,3,7] | ||
+ | $set: | ||
+ | $unset: | ||
+ | $rename: | ||
+ | |||
+ | $exists: | ||
+ | $gt:1 | ||
+ | $gte:1 | ||
+ | $lt:1 | ||
+ | $lte:1 | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Import/ | ||
+ | |||
+ | |||
+ | <code javascript> | ||
+ | // import csv into empty collection | ||
+ | mongoimport --db userdb --collection user --type csv --headerline --file C: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | // add csv to existing collection | ||
+ | mongoimport --db userdb --collection users --type csv --headerline --mode merge --file C: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | // import json into empty collection | ||
+ | mongoimport --db proj --collection docs --type json --file C: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | //export an exixting collection | ||
+ | mongoexport.exe /db:userdb / | ||
+ | </ | ||
+ | ===== Find Examples ===== | ||
+ | |||
+ | find(< | ||
+ | <code javascript> | ||
+ | db.user.find({age: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | db.user.find({$and: | ||
+ | </ | ||
+ | |||
+ | find(< | ||
+ | <code javascript> | ||
+ | db.User.find({age: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | //Return only the first_name and surname attributes of all documents | ||
+ | db.User.find({}, | ||
+ | </ | ||
+ | |||
+ | <code mongo> | ||
+ | db.student.find({address: | ||
+ | </ | ||
+ | ===== Update Examples ===== | ||
+ | |||
+ | **Note the use of <color # | ||
+ | <code javascript> | ||
+ | db.users.update({_id: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | db.users.update({$and: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | db.users.update({_id: | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | db.users.update({_id: | ||
+ | </ | ||
+ | <code javascript> | ||
+ | db.users.updateMany({}, | ||
+ | </ | ||
+ | |||
+ | ===== Aggregate Examples ===== | ||
+ | |||
+ | db.collection.aggregate ((https:// | ||
+ | |||
+ | <code mongo> | ||
+ | db.collection.aggregate([{$match: | ||
+ | </ | ||
+ | |||
+ | ==== $group ==== | ||
+ | |||
+ | <color # | ||
+ | <code mongo> | ||
+ | //Get the average gpa for all students | ||
+ | db.users.aggregate([{$group: | ||
+ | </ | ||
+ | <code mongo> | ||
+ | //Get the Maximum GPA per age group | ||
+ | db.march8.aggregate([{$group: | ||
+ | </ | ||
+ | <code mongo> | ||
+ | //To sort: $sort | ||
+ | db.march8.aggregate([{$group: | ||
+ | </ | ||
+ | ==== $lookup==== | ||
+ | |||
+ | **$lookup**: | ||
+ | |||
+ | ^ from: |< | ||
+ | ^ localField: |<field from the input documents> | ||
+ | ^ foreignField: | ||
+ | ^ as: |<output array field>| | ||
+ | |||
+ | <code mongo> | ||
+ | // .aggregate([{$lookup}]) | ||
+ | db.docs.aggregate([{$lookup: | ||
+ | </ | ||
+ | <code mongo> | ||
+ | // .aggregate([{$match}, | ||
+ | db.cityinfo.aggregate([{$match: | ||
+ | </ | ||
+ | <code mongo> | ||
+ | // .aggregate([{$match}, | ||
+ | db.cityinfo.aggregate([{$match: | ||
+ | </ | ||
+ | ===== Query, Update, Options ===== | ||
+ | |||
+ | db.collection.update ((https:// | ||
+ | |||
+ | <code mongodb> | ||
+ | db.collection.update( | ||
+ | < | ||
+ | < | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | ===== Indexing ===== | ||
+ | <code mongo> | ||
+ | db.collection.getIndexes() | ||
+ | </ | ||
+ | <code mongo> | ||
+ | db.user.createIndex({age: | ||
+ | </ | ||
+ | <code mongo> | ||
+ | db.collection.dropIndex({age: | ||
+ | </ | ||
+ | **Note:** The index on **_id** cannot be dropped |
help/mongodb/refsheet.txt · Last modified: 2020/06/20 14:39 by 127.0.0.1