mongodbmongo-shellmongosh

Rendering NumberLong in a single field when using mongosh


I have a collection c in DB named ftest in MongoDB. A document has been created in that collection with a NumberLong field this way:

db.c.insert({x: NumberLong("1")})

I get the content of the collection this way (based in this approach):

$ mongosh mongodb://localhost:27017/ftest --eval 'JSON.stringify(db.c.find().toArray(), null, " ")' --quiet
[
 {
  "_id": "6580316ed1c208e8c6059954",
  "x": {
   "low": 1,
   "high": 0,
   "unsigned": false
  }
 }
]

Comparing with how mongo legacy shell works:

$ mongo mongodb://localhost:27017/ftest --eval 'JSON.stringify(db.c.find().toArray(), null, " ")' --quiet
[
 {
  "_id": {
   "$oid": "6580316ed1c208e8c6059954"
  },
  "x": {
   "$numberLong": "1"
  }
 }
]

So:

Thanks!


Solution

  • Thanks to @WernfriedDomscheit feedback, using

    $ mongosh mongodb://localhost:27017/ftest --eval 'EJSON.stringify(db.c.find().toArray(), null, " ")' --quiet
    

    I get

    [
     {
      "_id": {
       "$oid": "6580316ed1c208e8c6059954"
      },
      "x": 1
     }
    ]
    

    which cover perfectly my use case.