Skip to content

Instantly share code, notes, and snippets.

@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],
> 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: {