mongo snippets: array cheatsheet

Cheatsheet - Array Of Values

A short cheat sheet for working with arrays in mongodb queries and aggregations. Inspired by: Mongo Doc - Update Array Operator

document structure#

For all following examples we assume a collection named “arrays” with at least on document of the following structure:

{
  "tags": [
    "tag a",
    "tag b",
    "tag c"
  ]
}

create#

Creation is very straight forward so we will skip this part here.

query#

Basic query operation on elements.

contains an element

db.arrays.find({
    tags: "tag a",
  }).toArray();

contains on of multi elements

db.arrays.find({
    tags: {
      $in: ["tag a", "tag c"],
    },
  }).toArray();

contains all elements

db.arrays.find({
    tags: { 
      $all: ["tag a", "tag c"] 
    },
  }).toArray();

does not contain

db.arrays.find({
    tags: {$ne: "tag c"},
  }).toArray();

or with multiple values:

db.arrays.find({
    tags: {$nin: ["tag a", "tag c"]},
  }).toArray();

other conditions

There are other conditions than equality like greater/less and some more. For a full list pleas refere to the documentation:

update#

Update will only update a single document, be aware of that. If you want to modifiy all documents use {multi: true} or db.collection.updateMany().

add#

add one element

db.arrays.update(
  {},
  {
    $push: {
      tags: "tag x",
    },
  }
);

add multi elements

db.arrays.update(
  {},
  {
    $push: {
      tags: {
        $each: [
          "tag y",
          "tag z",
        ],
      },
    },
  }
);

add only if not existing

db.arrays.update(
  {},
  {
    $addToSet: {
      tags: {
        $each: [
          "tag c",
          "tag d",
        ],
      },
    },
  }
);

find and replace values#

find first occurence and change that value

db.arrays.updateMany(
  { tags: "tag a" },
  {
    $set: {
      "tags.$":
        "tag c",
    },
  }
);

find all occurence and change all of their values

db.arrays.updateMany(
  {},
  {
    $set: {
      "tags.$[filter]":
        "tag c",
    },
  },
  {
    arrayFilters: [
      {
        filter:
          "tag a",
      },
    ],
  }
);

More on arrayFilters you can find in Mongo Doc - Positional Filter.

override whole array of a single document#

db.arrays.update(
  {},
  {
    tags: [
      "tag a",
      "tag b",
      "tag c",
    ],
  }
);

remove#

This removes all matching tags, also if they are doubled inserted.

db.arrays.update(
  {},
  {
    $pull: {
      tags: "tag a",
    },
  },
  { multi: true }
);

remove multiple values

db.arrays.update(
  {},
  {
    $pull: {
      tags: {
        $in: [
          "tag a",
          "tag b",
        ],
      },
    },
  }
);

sorting#

You can sort your array after inserting a value.

sorting by with push

db.arrays.update(
  {},
  {
    $push: {
      tags: {
        $each: [
          "tags x",
        ],
        $sort: -1,
      },
    },
  }
);

sorting fields without inserting

You can make an empty push and let monog db sort the fields.

db.arrays.update(
  {},
  {
    $push: {
      tags: {
        $each: [],
        $sort: 1,
      },
    },
  }
);