Michalis
Michalis

Reputation: 6926

Mongo - Empty object to query string

transporters.count({ '$and': [ {} ] })

transporters.count({ })

Is it the same?

Is it possible the empty object cause speed issues?

Upvotes: 1

Views: 89

Answers (1)

kevinadi
kevinadi

Reputation: 13795

Yes both queries are considered the same.

This can be seen in the explain output of both queries:

> db.test.explain().count({})
{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "test.test",
    "indexFilterSet": false,
    "winningPlan": {
      "stage": "COUNT"
    },
    "rejectedPlans": [ ]
  },
....

and:

> db.test.explain().count({$and:[{}]})
{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "test.test",
    "indexFilterSet": false,
    "winningPlan": {
      "stage": "COUNT"
    },
    "rejectedPlans": [ ]
  },
....

The explain() output of both queries are identical, meaning both queries will execute the same way.

Upvotes: 1

Related Questions