Skip to content

Instantly share code, notes, and snippets.

@come-maiz
Last active June 14, 2021 10:46
Show Gist options
  • Save come-maiz/616500568664d14a30669ecdf468d164 to your computer and use it in GitHub Desktop.
Save come-maiz/616500568664d14a30669ecdf468d164 to your computer and use it in GitHub Desktop.
MongoDB snap testing

Smoke tests for the MongoDB snap

(Based on https://docs.mongodb.com/getting-started/shell/)

Search for the mongodb snap:

$ snap find mongodb
Name         Version    Developer  Notes  Summary
robomongo    0.9.0-rc9  frankban   -      MongoDB management tool
mongo22      2.2.7      niemeyer   -      MongoDB document-oriented database
mongo24      2.4.14     niemeyer   -      MongoDB document-oriented database
mongo32      3.2.7      niemeyer   -      MongoDB document-oriented database
mongo30      3.0.12     niemeyer   -      MongoDB document-oriented database
mongo26      2.6.12     niemeyer   -      MongoDB document-oriented database
mongo33      3.3.9      niemeyer   -      MongoDB document-oriented database
wekan-ondra  0.10.1-1   ondra      -      The open-source Trello-like kanban

Install the snap:

$ sudo snap install mongo33
[sudo] password for elopio: 
mongo33 (stable) 3.3.9 from 'niemeyer' installed

Import example dataset:

$ wget https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
$ mongo33.import --port 33017 --db test --collection restaurants --drop --file primer-dataset.json
2016-11-01T18:13:43.973-0600	connected to: localhost:33017
2016-11-01T18:13:43.974-0600	dropping: test.restaurants
2016-11-01T18:13:44.967-0600	imported 25359 documents

Start the shell:

$ mongo33
MongoDB shell version: 3.3.9
connecting to: 127.0.0.1:33017/test
MongoDB server version: 3.3.9
Welcome to the MongoDB shell.
[...]
> 

Print the help:

> help
	db.help()                    help on db methods
    db.mycoll.help()             help on collection methods
    sh.help()                    sharding helpers
[...]

Switch to the test databse:

> use test
switched to db test

Insert a document:

> db.restaurants.insert(
   {
      "address" : {
         "street" : "2 Avenue",
         "zip code" : "10075",
         "building" : "1480",
         "coord" : [ -73.9557413, 40.7720266 ]
      },
      "borough" : "Manhattan",
     "cuisine" : "Italian",
     "grades" : [
         {
            "date" : ISODate("2014-10-01T00:00:00Z"),
            "grade" : "A",
            "score" : 11
         },
         {
            "date" : ISODate("2014-01-16T00:00:00Z"),
            "grade" : "B",
            "score" : 17
         }
      ],
      "name" : "Vella",
      "restaurant_id" : "41704620"
   }
)
WriteResult({ "nInserted" : 1 })

Query for All Documents in a Collection:

> db.restaurants.find()
{ "_id" : ObjectId("58192fb8246ac3f1f52eb2db"), "address" : { "building" : "1007", "coord" : [ -73.856077, 40.848447 ], "street" : "Morris Park Ave", "zipcode" : "10462" }, "borough" : "Bronx", "cuisine" : "Bakery", "grades" : [ { "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-09-11T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-23T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-03-10T00:00:00Z"), "grade" : "B", "score" : 14 } ], "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" }
[...]
Type "it" for more

Query by a Top Level Field:

> db.restaurants.find( { "borough": "Manhattan" } )
{ "_id" : ObjectId("58192fb8246ac3f1f52eb2dd"), "address" : { "building" : "351", "coord" : [ -73.98513559999999, 40.7676919 ], "street" : "West   57 Street", "zipcode" : "10019" }, "borough" : "Manhattan", "cuisine" : "Irish", "grades" : [ { "date" : ISODate("2014-09-06T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-07-22T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-07-31T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2011-12-29T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Dj Reynolds Pub And Restaurant", "restaurant_id" : "30191841" }
[...]
Type "it" for more

Query by a Field in an Embedded Document:

> db.restaurants.find( { "address.zipcode": "10075" } )
{ "_id" : ObjectId("58192fb8246ac3f1f52eb3f2"), "address" : { "building" : "1022", "coord" : [ -73.9624221, 40.77620840000001 ], "street" : "Madison Avenue", "zipcode" : "10075" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-02-20T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2013-08-15T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2013-03-08T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-06-20T00:00:00Z"), "grade" : "B", "score" : 16 }, { "date" : ISODate("2011-12-08T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2011-07-28T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-02-24T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Nectar Coffee Shop", "restaurant_id" : "40369759" }
[...]
Type "it" for more

Query by a Field in an Array:

> db.restaurants.find( { "grades.grade": "B" } )
{ "_id" : ObjectId("58192fb8246ac3f1f52eb2db"), "address" : { "building" : "1007", "coord" : [ -73.856077, 40.848447 ], "street" : "Morris Park Ave", "zipcode" : "10462" }, "borough" : "Bronx", "cuisine" : "Bakery", "grades" : [ { "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "A", "score" : 2 }, { "date" : ISODate("2013-09-11T00:00:00Z"), "grade" : "A", "score" : 6 }, { "date" : ISODate("2013-01-24T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-23T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-03-10T00:00:00Z"), "grade" : "B", "score" : 14 } ], "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" }
[...]
Type "it" for more

Update Top-Level Fields:

> db.restaurants.update(
    { "name" : "Juni" },
    {
      $set: { "cuisine": "American (New)" },
      $currentDate: { "lastModified": true }
    }
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Update an Embedded Field:

> db.restaurants.update(
  { "restaurant_id" : "41156888" },
  { $set: { "address.street": "East 31st Street" } }
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Update Multiple Documents:

> db.restaurants.update(
  { "address.zipcode": "10016", cuisine: "Other" },
  {
    $set: { cuisine: "Category To Be Determined" },
    $currentDate: { "lastModified": true }
  },
  { multi: true}
)
WriteResult({ "nMatched" : 20, "nUpserted" : 0, "nModified" : 20 })

Replace a Document:

> db.restaurants.update(
   { "restaurant_id" : "41704620" },
   {
     "name" : "Vella 2",
     "address" : {
              "coord" : [ -73.9557413, 40.7720266 ],
              "building" : "1480",
              "street" : "2 Avenue",
              "zipcode" : "10075"
     }
   }
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Remove All Documents That Match a Condition:

 > db.restaurants.remove( { "borough": "Manhattan" } )
 WriteResult({ "nRemoved" : 10259 })

Use the justOne Option:

> db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
WriteResult({ "nRemoved" : 1 })

Remove All Documents:

> db.restaurants.remove( { } )
WriteResult({ "nRemoved" : 15100 })

Drop a Collection:

> db.restaurants.drop()
true

Import the dataset again:

> quit()
$ mongo33.import --port 33017 --db test --collection restaurants --drop --file primer-dataset.json
$ mongo33
$ use test

Group Documents by a Field and Calculate Count:

> db.restaurants.aggregate(
   [
     { $group: { "_id": "$borough", "count": { $sum: 1 } } }
   ]
);
{ "_id" : "Missing", "count" : 51 }
{ "_id" : "Staten Island", "count" : 969 }
{ "_id" : "Brooklyn", "count" : 6086 }
{ "_id" : "Bronx", "count" : 2338 }
{ "_id" : "Queens", "count" : 5656 }
{ "_id" : "Manhattan", "count" : 10259 }

Filter and Group Documents:

> db.restaurants.aggregate(
   [
     { $match: { "borough": "Queens", "cuisine": "Brazilian" } },
     { $group: { "_id": "$address.zipcode" , "count": { $sum: 1 } } }
   ]
);
{ "_id" : "11377", "count" : 1 }
{ "_id" : "11103", "count" : 1 }
{ "_id" : "11106", "count" : 3 }
{ "_id" : "11368", "count" : 1 }
{ "_id" : "11101", "count" : 2 }

Create a Single-Field Index:

> db.restaurants.createIndex( { "cuisine": 1 } )
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

Create a compound index:

> db.restaurants.createIndex( { "cuisine": 1, "address.zipcode": -1 } )
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}
@come-maiz
Copy link
Author

Tested in a clean and up-to-date ubuntu xenial classic kvm machine. No issues found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment