Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB v6.0.2 or mongoplayground bug? #153

Open
rickhg12hs opened this issue Nov 8, 2022 · 3 comments
Open

MongoDB v6.0.2 or mongoplayground bug? #153

rickhg12hs opened this issue Nov 8, 2022 · 3 comments

Comments

@rickhg12hs
Copy link
Contributor

rickhg12hs commented Nov 8, 2022

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 rickhg12hs changed the title MongoDB or mongoplayground bug? MongoDB v6.0.2 or mongoplayground bug? Nov 8, 2022
@rickhg12hs
Copy link
Contributor Author

rickhg12hs commented Nov 10, 2022

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.

@feliixx
Copy link
Owner

feliixx commented Nov 14, 2022

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!

@johachi
Copy link

johachi commented Feb 7, 2024

Could this also be related to #171 regarding getting inconsistent results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants