Last active
January 10, 2017 06:58
-
-
Save rtoal/5184739 to your computer and use it in GitHub Desktop.
Getting started with the mongo shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
show dbs | |
use hello | |
db.people.insert({name: "Abdul", age: 22, likes: ["skiing", "hiking"]}) | |
db.people.insert({name: "Barbara", age: 20, country: "UK"}) | |
db.people.insert({name: "Chi", age: 40, country: "CN", | |
job: {title: "welder", salary: {currency: "EUR", amount: 50000}}}) | |
db.people.insert({name: "Danielle", country: "FR", | |
likes: ["cooking", "racing", "skiing"]}) | |
db.people.count() | |
db.people.find() | |
db.people.find({age: 20}) | |
db.people.find({age: {$lt: 30}}) | |
db.people.find({country: {$in: ["CN", "JP", "FR"]}}) | |
db.people.find({"job.title": "welder"}) | |
db.people.find({job: {$exists: 1}}) | |
db.people.find({country: {$exists: 0}}) | |
db.people.find({age: {$lt: 30}}, {name: 1}) | |
db.people.find({age: {$lt: 30}}, {_id: 0, name: 1}) | |
db.people.find({}, {_id: 0, name: 1, country: 1}) | |
db.people.find({}, {_id: 0, name: 1, country: 1}).sort({name: -1}) | |
db.people.update({name: "Barbara"}, {$set: {likes: ["hiking"]}}) | |
db.people.find({likes: "hiking"}) | |
db.people.distinct("likes") | |
db.people.distinct("country") | |
db.people.find({}, {_id: 0, name: 1, likes: 1}).forEach( | |
function (d) {print(d.name + " " + d.likes)}) | |
db.people.insert({name: "Etienne", country: "FR", likes: ["scuba"]}) | |
db.people.update({country: "FR"}, {$addToSet: {likes: "wine"}}) | |
db.people.find({}, {_id: 0, name: 1, likes: 1}).forEach( | |
function (d) {print(d.name + " " + d.likes)}) | |
db.people.update({country: "FR"}, {$addToSet: {likes: "wine"}}, {multi: true}) | |
db.people.find({}, {_id: 0, name: 1, likes: 1}).forEach( | |
function (d) {print(d.name + " " + d.likes)}) | |
db.people.count() | |
db.people.update({name: "Faye"}, {$set: {age: 3}}) | |
db.people.count() | |
db.people.update({name: "Faye"}, {$set: {age: 3}}, {upsert: true}) | |
db.people.count() | |
db.people.update({name: "Chi"}, {$addToSet: {likes: "swimming"}}, | |
{upsert: true}) | |
db.people.update({name: "Abdul"}, {$set: {country: "TN"}}) | |
db.people.aggregate({$group: {_id: "$country", num_people: {$sum: 1}}}) | |
db.people.aggregate([ | |
{$unwind : "$likes"}, | |
{$group : {_id : "$likes", number: {$sum : 1}}}, | |
{$sort : {number : -1}}, | |
{$limit : 5} | |
]) | |
db.people.aggregate([ | |
{$unwind : "$likes"}, | |
{$group : {_id : {$toUpper: "$likes"}, people: {$addToSet : "$name"}}}, | |
{$sort : {_id : 1}}, | |
{$limit : 10} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment