Json array in a single collection's document

i have a question: but how can i insert many json files inside one document collection? I have a ruby script connected with mongoDB which generate json files for each ID product. In mongo i should want a structure like this:

Id(document's name) : {

many json for same ID

} 

how can i get this structure in ruby?

DB’s name is “test_db” and collection’s name is “test_coll”

Hi @gioele_valori and welcome to MongoDB community forums!!

If I understand your question correctly, you are trying to add a JSON file into the collection. If my understanding is correct, you can either make use of the MongoDB tools and use the mongoimport to import the json file.
The other way to solve at the application end would be by making use of the file system.

This can be using using

  1. insertMany
  2. bulkWrite
  3. Reading a JSON file

Below is the example code using insertMany.

require 'mongo'
require 'json'
# Initialize MongoDB Connection
client = Mongo::Client.new(['localhost:27017'], database: 'test_db')
collection = client[:test_coll]

# Generate JSON Data for IDs
id1_data = [
  { "key1": "value1", "key2": { "key5": "value3", "key6": "value6"} },
  { "key1": "value3", "key2": "value4" }
]

id2_data = [
  { "key1": "value5", "key2": "value6" },
  { "key1": "value7", "key2": "value8" }
]

# Insert Data for IDs
collection.insert_one({ _id: 'id1', data: id1_data })
collection.insert_one({ _id: 'id2', data: id2_data })

# Repeat for other IDs as needed

# Closing the Connection
client.close

Please note that the following has been tested using Ruby Driver version 3.1.2 and would recommend to perform through testing before using in a production environment.
However if this is not you are looking for, could you help me a sample document that you wish to insert into the collection.

Regards
Aasawari