Skip to the content.

Table of Contents

Placeholders

MongoDB doen’t allow you to use fields with $ in name. Sometimes it’s necessary because of nature how MongoDB stores Date-Time & Long. That’s why Mongomery add something called placeholders that can be used to insert/assert data in JSON files

There is two types of Placeholders: insert & assert placeholders.

Insert placeholders

$insertLocalDateTime(yyyy-MM-ddTHH:mm:ss) to insert java.time.LocalDateTime
{
  "TestCollection": [
    {
      "stringField": "some value",
      "localDateTimeField": "$insertLocalDateTime(2020-02-02T11:11:11)"
    }
  ]
}
$insertLocalDate(yyyy-MM-dd) to insert java.time.LocalDate
{
  "TestCollection": [
    {
      "localDate": "$insertLocalDate(2222-02-02)"
    }
  ]
}
$insertLong(1242) to insert long
{
  "TestCollection": [
    {
      "stringField": "some value",
      "longField": "$insertLong(215345325523512)"
    }
  ]
}

Assert placeholders

$anyObject() - asserts that field is of type object with any number of fields

assert JSON file has following content:

{
  "TestCollection": [
    {
      "objectField": "$anyObject()"
    }
  ]
}

then any of the bellow objects will pass assert:

{
  "TestCollection": [
    {
      "objectField": { }
    }
  ]
}
{
  "TestCollection": [
    {
      "objectField": {
        "field": 1
      }
    }
  ]
}
{
  "TestCollection": [
    {
      "objectField": {
        "field": 1,
        "field2": 2
      }
    }
  ]
}

test will fail on any of below condition:

$anyObject(23) - assert that field is of object type and has specific amount of direct inner fields

assert JSON file has following content:

{
  "TestCollection": [
    {
      "objectField": "$anyObject(2)"
    }
  ]
}

then bellow object can pass this assert. note: there is no check of fields’ names for object

{
  "TestCollection": [
    {
      "objectField": {
        "field": 1,
        "field2": 2
      }
    }
  ]
}
$anyString() assert that field is of string type with any content

assert JSON file has following content:

{
  "TestCollection": [
    {
      "stringField": "$anyString()"
    }
  ]
}

then any of the bellow objects will pass assert:

{
  "TestCollection": [
    {
      "stringField": ""
    }
  ]
}
{
  "TestCollection": [
    {
      "stringField": "some long string"
    }
  ]
}
$anyString(/regex/) assert that field is of string type and it’s content match specific pattern

assert JSON file has following content:

{
  "TestCollection": [
    {
      "stringField": "$anyString(/.*_endWith/)"
    }
  ]
}

then any of the bellow objects will pass assert:

{
  "TestCollection": [
    {
      "stringField": "some long content that _endWith"
    }
  ]
}
{
  "TestCollection": [
    {
      "stringField": "_endWith"
    }
  ]
}
$anyLongValue() assert that field if of long type with any value

assert JSON file has following content:

{
  "TestCollection": [
    {
      "longField": "$anyLongValue()"
    }
  ]
}

then any of the bellow objects will pass assert:

{
  "TestCollection": [
    {
      "longField": "$insertLong(123)"
    }
  ]
}
{
  "TestCollection": [
    {
      "longField": {
        "$numberLong": 53242342   // this is how MongoDB stores long 
      }
    }
  ]
}
$anyDate() assert that field if of Date-Time type with any value

assert JSON file has following content:

{
  "TestCollection": [
    {
      "dateField": "$anyDate()"
    }
  ]
}

then any of the bellow objects will pass assert:

{
  "TestCollection": [
    {
      "dateField": "$insertLocalDateTime(2222-03-04T05:06:07)"
    }
  ]
}
{
  "TestCollection": [
    {
      "dateField": "$insertLocalDate(2222-03-04)"
    }
  ]
}
{
  "TestCollection": [
    {
      "dateField": {
        "$date": 12512312531231   // this is how MongoDB stores Date-Time objects 
      }
    }
  ]
}
$eqLongValue(123) assert that field of long type and it has specific value
 {
   "TestCollection": [
     {
       "longField": "$eqLongValue(123)"
     }
   ]
 }

then any of the bellow objects will pass assert:

 {
   "TestCollection": [
     {
       "longField": "$insertLong(123)"
     }
   ]
 }
 {
   "TestCollection": [
     {
       "longField": {
          "$numberLong": 123 
       }
     }
   ]
 }
$eqLocalDateTimeValue(yyyy-MM-ddTHH:mm:ss) assert that Date-Time field has specific value

assert JSON file has following content:

{
  "TestCollection": [
    {
      "dateField": "$eqLocalDateTimeValue(2020-03-04T05:06:07)"
    }
  ]
}

then any of the bellow objects will pass assert:

{
  "TestCollection": [
    {
      "dateField": "$insertLocalDateTime(2222-03-04T05:06:07)"
    }
  ]
}
{
  "TestCollection": [
    {
      "dateField": {
        "$date": 12512312531231   // 2020-03-04T05:06:07 in millis. value depends on timezone you use in connection string
      }
    }
  ]
}
$eqLocalDateValue(yyyy-MM-dd) assert that Date field has specific value

assert JSON file has following content:

{
  "TestCollection": [
    {
      "dateField": "$eqLocalDateValue(2020-03-04)"
    }
  ]
}

then any of the bellow objects will pass assert:

{
  "TestCollection": [
    {
      "dateField": "$insertLocalDate(2222-03-04)"
    }
  ]
}
{
  "TestCollection": [
    {
      "dateField": {
        "$date": 12512312531231   // 2020-03-04 in millis. value depends on timezone you use in connection string
      }
    }
  ]
}