0

Shell Mode? https://docs.mongodb.com/manual/reference/mongodb-extended-json/#oid

enter image description here

How to enable shell mode so I can have "_id" only in the JSON output instead of the following.

"_id": {
    "$oid": "5d3bd0f8454e59408cffdb57"
},

I prefer to have JSON output as follows.

"_id": "5d3bd0f8454e59408cffdb57"

I've several levels of nested records in my JSON output, and then each nested records with its own nested $oids are exploding the depth of my payload.

Edit

Following discussion, from the linked answer, here's the output from each mode.

JsonWriterSettings shellMode = JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build();
"_id": ObjectId("5d3bd0f8454e59408cffdb57")

JsonWriterSettings strictMode = JsonWriterSettings.builder().outputMode(JsonMode.STRICT).build();
"_id": {"$oid": "5d3bd0f8454e59408cffdb57"}

JsonWriterSettings extendedMode = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
 "_id": {"$oid": "5d3bd0f8454e59408cffdb57"}

JsonWriterSettings relaxed = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
"_id": {"$oid": "5d3bd0f8454e59408cffdb57"}

Complete Code

JsonWriterSettings relaxed = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();

try (MongoCursor<Document> cursor = ((MongoCollection)data).find().iterator()) {
    response.getWriter().write("[");
    while (cursor.hasNext()) {
        response.getWriter().write(cursor.next().toJson(relaxed) + (cursor.hasNext() ? "," : ""));
    }
    response.getWriter().write("]");
}
AppDeveloper
  • 1,816
  • 7
  • 24
  • 49
  • 1
    Enable JSON output from what exactly? `mongoexport`? Or something else? You also have your terminology the wrong way around. As per the document you refer to: `{ "$oid": "" }` **is strict mode** ( not shell mode ) and the `ObjectId()` function helpers displayed are from the **mongo shell**. Just like it actually says. – Neil Lunn Jul 27 '19 at 08:27
  • @NeilLunn converting each record and it's id to string is not an option, since I'm using Java, to give you an idea `response.getWriter().write(((Document)doc).toJson());`, another use case is when I'm serializing whole collection. – AppDeveloper Jul 27 '19 at 08:38
  • 1
    I was actually prompting you to add **useful** information to your question such as ***".. I'm using Java.."***, which is currently not present in the question or within the tags ( so edit your question with that information and the right people see it). Also FYI the *default* output format from the Java Driver implementation of `toJson()` is **strict mode** as a string as mentioned. But there's more than one way to convert an object to a JSON string. Hence add **java** to the provided Google search and do some more reading. Plenty of others have done this before. – Neil Lunn Jul 27 '19 at 09:13
  • @NeilLunn Thanks for your feedback. please see my edits, I got closer with the shell mode but still `ObjectId` is in the way. – AppDeveloper Jul 27 '19 at 09:55
  • Please also see my `complete code` section, just in case my method of looping through records is wrong, and there's some brief method? – AppDeveloper Jul 27 '19 at 09:58
  • 1
    *Also* read the answer on the marked duplicate more carefully. It clearly says **RELAXED** is what you are actually after. Much as I have repeated twice already that *shell* is the **wrong term** for what you actually want. – Neil Lunn Jul 27 '19 at 09:59
  • sure, sorry for the confusion. I've used relaxed but it's showing `$oid` nested within `_id`, updating complete code (do you like it the way it's done via while loop?) – AppDeveloper Jul 27 '19 at 10:07
  • 1
    Again. Please actually read the **answers** ( as in there is more than one ) and run the code examples shown. The answer is there and it's very clear if you actually look at the output from **ALL** the listed output options and see what they actually do. – Neil Lunn Jul 27 '19 at 10:15
  • Yes I ran all the modes, I used gil.fernandes answer since the accepted answer was deprecated. I tested with all shell, extended and relaxed mode. extended and relaxed mode still shows `$oid` and shell mode displays `"_id": ObjectId("5d3bd0f8454e59408cffdb57"),`. I did several tests. – AppDeveloper Jul 27 '19 at 10:19

0 Answers0