Skip to content

Instantly share code, notes, and snippets.

@npentrel
npentrel / pokemon.json
Last active November 14, 2023 12:35
Pokemon data from the pokeapi.co in json format. It is formatted to allows for easy importing into MongoDB.
{"_id":"557a723880a20c9db3bc31c2","pkdx_id":1,"national_id":1,"name":"Bulbasaur","__v":3,"image_url":"http://pokeapi.co/media/img/1.png","description":"Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger. Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger.","art_url":"http://assets22.pokemon.com/assets/cms2/img/pokedex/full/001.png","types":["poison","grass"],"evolutions":[{"level":16,"method":"level_up","to":"Ivysaur","_id":"557a723880a20c9db3bc31c3"}]}
{"_id":"557a723980a20c9db3bc31e3","pkdx_id":2,"national_id":2,"name":"Ivysaur","__v":4,"image_url":"http://pokeapi.co/media/img/2.png","description":"There is a bud on this Pokémon's back. To support its weight, Ivysaur's legs and trunk grow thick and strong. If it starts spending more time lying in the sunlight, it's a sign that the bud will bloom into a large flower soon.
@npentrel
npentrel / insert.js
Last active August 3, 2018 21:14
Use aggregation expressions in queries with $expr - Insert operation
> db.accounts.insertMany([
   {"_id": 1, "credits": 5000, "expenses": [2000, 2000]},
   {"_id": 2, "credits": 4000, "expenses": [1000, 4000, 2000]},
   {"_id": 3, "credits": 3000, "expenses": [1500, 750]},
   {"_id": 4, "credits": 2000, "expenses": [2500, 750]}
])
@npentrel
npentrel / find.js
Created August 3, 2018 21:17
Use aggregation expressions in queries with $expr - find operation
> db.accounts.find({
     $expr : {
         $gt : [
             { $sum : ["$expenses"] },
             "$credits"
         ]
     }
 })
{ "_id" : 2, "credits" : 4000, "expenses" : [ 1000, 4000, 2000 ] }
{ "_id" : 4, "credits" : 2000, "expenses" : [ 2500, 750 ] }
@npentrel
npentrel / expr.js
Created August 3, 2018 21:20
Use aggregation expressions in queries with $expr - insert, create index, and find queries
> db.accountsWithDate.insertMany([
   {"_id": 1, "credits": 5000, "expenses": [2000, 2000], "date": new ISODate("2018-05-01T12:42:10.318Z")},
   {"_id": 2, "credits": 4000, "expenses": [1000, 4000, 2000], "date":  new ISODate("2018-05-01T22:21:50.015Z")},
   {"_id": 3, "credits": 3000, "expenses": [1500, 750], "date": new ISODate("2018-05-02T07:01:20.259Z")},
   {"_id": 4, "credits": 2000, "expenses": [2500, 750], "date": new ISODate("2018-05-03T16:39:05.120Z")}
 ]) 
> db.accountsWithDate.createIndex({"date": 1})
> db.accountsWithDate.find({
> db.alphabet.insert({
"_id": "00000",
"array": ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
})
// We missed the 'i'!
// Let's add it in the position before the last element.
> db.alphabet.update(
{ "_id": "00000" },
{
$push: {
> db.alphabet.insert({
"_id": "11111",
"array": ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j']
})
// We missed the 'i'!
// Let's add it in the position before the last element.
> db.alphabet.update(
{ "_id": "11111" },
{
$push: {
> db.alphabet.insert({
"_id": "22222",
"array": ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j']
})
// We missed the 'i'!
// Let's add it in the position before the last element.
> db.alphabet.update(
{ "_id": "22222" },
{
$push: {
> db.alphabet.insert({
"_id": "33333",
"array": ['a', 'b', 'c', 'd', 'e', 'i', 'j']
})
// We missed the 'f', the 'g', and the 'h'!
// Let's add them in the position before the last two elements.
> db.alphabet.update(
{ "_id": "33333" },
{
$push: {
> db.tracking.insert({
"_id": "44444",
"recentItems": ['Apples', 'Bananas', 'Cherries']
})
> db.tracking.update(
{ "_id": "44444" },
{
$push: {
"recentItems": {
$each: ['Dates'],
> db.game.insert({
"_id": "55555",
"scores": [223, 220, 190, 176, 151, 139, 124, 123, 80, 57]
})
> db.game.update(
{ "_id": "55555" },
{
$push: {
scores: {
$each: [72],