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
> db.alphabet.insert({ | |
"_id": "66666", | |
"array": ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] | |
}) | |
// We missed the 'a'! | |
// Let's add it to the beginning by specifying $position: -0. | |
> db.alphabet.update( | |
{ "_id": "66666" }, | |
{ | |
$push: { |
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
> 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], |
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
> db.tracking.insert({ | |
"_id": "44444", | |
"recentItems": ['Apples', 'Bananas', 'Cherries'] | |
}) | |
> db.tracking.update( | |
{ "_id": "44444" }, | |
{ | |
$push: { | |
"recentItems": { | |
$each: ['Dates'], |
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
> 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: { |
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
> 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: { |
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
> 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: { |
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
> 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: { |
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
> 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({ |
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
> db.accounts.find({ | |
$expr : { | |
$gt : [ | |
{ $sum : ["$expenses"] }, | |
"$credits" | |
] | |
} | |
}) | |
{ "_id" : 2, "credits" : 4000, "expenses" : [ 1000, 4000, 2000 ] } | |
{ "_id" : 4, "credits" : 2000, "expenses" : [ 2500, 750 ] } |
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
> 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]} | |
]) |