JSON to MongoDB Query

JSON Tools

How to use the JSON to MongoDB Query

Generate MongoDB queries from JSON:

1

Paste your JSON

Paste a JSON object for insertOne or find, or a JSON array of objects for insertMany. The tool auto-switches to insertMany when an array is detected.

2

Choose query type

Select insertOne to insert a single document, insertMany for bulk inserts, or find to generate a filter query from your JSON fields.

3

Set collection name

Enter the MongoDB collection name (e.g. users, products). It appears directly in the generated db.collection query.

4

Copy and run

Copy the query and run it in MongoDB Shell (mongosh), MongoDB Compass (MONGOSH tab), or paste into your Node.js driver using the mongodb npm package.


When to use this tool

Use this when working with MongoDB and need query syntax from JSON data:

  • Seeding a MongoDB development database from JSON fixture files
  • Converting REST API response JSON into MongoDB insert documents
  • Building MongoDB shell scripts from JSON data exports
  • Generating Mongoose create() calls from JSON test data
  • Importing JSON API responses into MongoDB Atlas using the shell or Compass

Frequently asked questions

Q:What query types does it generate?
The tool generates three query types: db.collection.insertOne() for a single JSON object, db.collection.insertMany() for a JSON array of objects, and db.collection.find() plus db.collection.findOne() for filter queries derived from your JSON's scalar fields.
Q:How are ObjectIds and dates handled?
Any 24-character hexadecimal string is automatically wrapped in ObjectId("...") since that's the standard MongoDB ObjectId format. ISO date strings (like 2024-01-15T10:30:00Z) are wrapped in ISODate("...") for correct MongoDB date storage. Both use MongoDB shell syntax compatible with mongosh and Compass.
Q:Can I use this with Mongoose?
Yes — the generated insertOne and insertMany syntax matches what you'd pass to Mongoose's Model.create() or Model.insertMany() methods. Strip the db.collection prefix and pass the document object directly to your Mongoose model method.
Q:How does the find query work?
The find query type generates a filter using only scalar fields (strings, numbers, booleans) from your JSON. Nested objects and arrays are excluded from the filter automatically since they require more complex query operators like $elemMatch or dot notation to query correctly.
Q:Can I run the output in MongoDB Compass?
Yes — open your collection in MongoDB Compass, click the MONGOSH tab at the bottom, and paste the generated query directly. For insertMany operations with large datasets, consider using mongoimport instead for better performance.
Q:Does it work with MongoDB Atlas?
Yes — the generated queries are standard MongoDB shell syntax and run without modification in MongoDB Atlas's built-in shell, Atlas App Services functions, and any Node.js application using the official mongodb driver or Mongoose.