mongoplayground icon indicating copy to clipboard operation
mongoplayground copied to clipboard

MongoDB v6.0.2 or mongoplayground bug?

Open rickhg12hs opened this issue 2 years ago • 3 comments

While exploring ways to retrieve or "$merge"/"$out" unique documents from a collection, I ran across the following "oddity".

mongoplayground.net example

mongoplayground mgodatagen configuration:

[
  {
    "collection": "collection",
    "count": 100,
    "content": {
      "key1": {
        "type": "int",
        "minInt": 0,
        "maxInt": 1
      },
      "key2": {
        "type": "int",
        "minInt": 0,
        "maxInt": 1
      }
    }
  }
]

Query:

db.collection.aggregate([
  {
    "$unset": "_id"
  },
  {
    "$group": {
      "_id": "$$ROOT"
    }
  },
  {
    "$replaceWith": "$_id"
  }
])

Result: (should just be 4 docs, but it's not - there are repeats)

[
  {
    "key1": 1,
    "key2": 1
  },
  {
    "key1": 1,
    "key2": 0
  },
  {
    "key1": 1,
    "key2": 0
  },
  {
    "key1": 1,
    "key2": 1
  },
  {
    "key1": 0,
    "key2": 0
  },
  {
    "key1": 0,
    "key2": 1
  },
  {
    "key1": 0,
    "key2": 0
  },
  {
    "key1": 0,
    "key2": 1
  }
]

Using the same collection and query on MongoDB Atlas, the expected output of just 4 documents was produced.

The MongoDB Atlas Cluster I used is at version 5.0.13 so maybe it's a MongoDB server regression?

rickhg12hs avatar Nov 08 '22 07:11 rickhg12hs

I have another data point, I think.

TLDR; I think object field ordering isn't preserved when creating the MongoDB collection.

Rather than splash some more configs and queries here, I just supply the mongoplayground.net links.

Here's a mongoplayground.net where the results are not consistent. Hitting run several times, sometimes the output is correct (essentially converting an array to a set) and sometimes the result has repeat elements in the array.

Here, the mongoplayground.net has the same query, except the objects in the array are rewritten. The result is consistent and correct.

Does this mean that documents written to the collection have randomly/inconsistently ordered objects?

This would also explain the "oddity" described in my first comment above.

rickhg12hs avatar Nov 10 '22 09:11 rickhg12hs

TLDR; I think object field ordering isn't preserved when creating the MongoDB collection.

Exactly. It's exactly what's happening here, for example in mongosh

use test
db.test.insertOne({a:1,b:0})
db.test.insertOne({b:0,a:1})
db.test.aggregate([ { "$unset": "_id" }, { "$group": { "_id": "$$ROOT" } }, { "$replaceWith": "$_id" }])

Result: [ { a: 1, b: 0 }, { b: 0, a: 1 } ]

Sadly, it's the driver that's responsible of this. It's actually the same root cause as #150... So another good reason to try to fix this field reordering issue!

feliixx avatar Nov 14 '22 20:11 feliixx

Could this also be related to https://github.com/feliixx/mongoplayground/issues/171 regarding getting inconsistent results?

johachi avatar Feb 07 '24 01:02 johachi