How to insert document or fields in nested documents in mongo

15,441

Using Shell

1.How will I create such nested document

db.systems.insert( [{"name": "sub_system_1", "data" : {"high" :["task1","task2"], "mid": ["task1","task2"], "low" : ["task1","task2"]}},{"name": "sub_system_2" ,"data": {"high" :["task1","task2"], "mid": ["task1","task2"], "low" : ["task1","task2"]}}])

2.How will I insert new sub_system in the systems collection

db.systems.insert( {"name": "sub_system_3", "data" : {"high" :["task1","task2"], "mid": ["task1","task2"], "low" : ["task1","task2"]}})

3.How will I insert new priority (high, mid, low) under sub_system document

// First you need to find the document using the name field
var sub_system_1 = db.systems.findOne({"name":"sub_system_1"})
// set your new priority
sub_system_1.data.new_priority = [ "task1", "task2" ]
// Save updated document 
db.systems.save(sub_system_1)

You can also do the update in one statement using the updateOne call please see below documentation https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#db.collection.updateOne

4.How will I insert new task in a specific sub_system and under a specific priority(high,low, mid) document

// First you need to find the document using the name field
var sub_system_1 = db.systems.findOne({"name":"sub_system_1"})
// Now we add a new task to priority low
sub_system_1.data.low[sub_system_1.data.low.length] = "task3"
// Save updated document 
db.systems.save(sub_system_1)

Using perl mongodb

#!/usr/bin/perl

use MongoDB::Connection;

my $mongodb_dbhost = 'mongodb.co.za';
my $mongodb_dbname = 'systems_production';
my $mongodb_dbtable = 'systems';

my $connection   =  MongoDB::Connection->new(host => $mongodb_dbhost);

my $collection = $connection->$mongodb_dbname->$mongodb_dbtable;

1. How will I create such nested document

my $data = $collection->insert( [
    {
       "name": "sub_system_1", "data" : {"high" :["task1","task2"], "mid":     ["task1","task2"], "low" : ["task1","task2"]}
},
{
    "name": "sub_system_2" ,"data": {"high" :["task1","task2"], "mid":    ["task1","task2"], "low" : ["task1","task2"]}
}
  ]); 

2. How will I insert new sub_system in the systems collection

  $data = $collection->insert({
      "name": "sub_system_3", 
   "data" : {"high" :["task1","task2"], "mid": ["task1","task2"], "low" :     ["task1","task2"]}
  });

3. How will I insert new priority (high, mid, low) under sub_system document

my $sub_system_1 = $connection->$mongodb_dbname->$mongodb_dbtable->find_one({'name' => 'sub_system_1'});
$sub_system_1->{data}->{new_priority} = [ "task1", "task2" ];
$collection->save($sub_system_1);

4. How will I insert new task in a specific sub_system and under a specific priority(high,low, mid) document

my $sub_system_1 = $connection->$mongodb_dbname->$mongodb_dbtable->find_one({'name' => 'sub_system_1'});
 $sub_system_1->{data}->{low}->[scalar(@$sub_system_1->{data}->{low})] = "task3";
 $collection->save($sub_system_1);
Share:
15,441

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I want collection named systems under which there is a document named sub_systems and under which there are three documents named high mid & low. And each high, mid and low contains separate multiple documents Like below

    systems: {
               sub_system_1 : {
                                high :{ {task1},
                                        {task2},
                                        .......
                                      },                                
                                mid  :{ {task1},
                                        {task2},
                                        .......
                                      },
                                low :{ {task1},
                                        {task2},
                                        .......
                                      },
                              },
               sub_system_2 : {
                                high :{ {task1},
                                        {task2},
                                        .......
                                      },                                
                                mid  :{ {task1},
                                        {task2},
                                        .......
                                      },
                                low :{ {task1},
                                        {task2},
                                        .......
                                      },
                              },
               ............
             }
    

    So following are the questions I'm having 1. How will I create such nested document. 2. How will I insert new sub_system in the systems collection. 3. How will I insert new priority (high, mid, low) under sub_system document. 4. How will I insert new task in a specific sub_system and under a specific priority(high,low, mid) document

    I want to know this using mongo shell and also with perl mongodb

  • Admin
    Admin about 7 years
    you created array inside high,mid and low and not a document which is totally opposite to what I asked for
  • djtsheps
    djtsheps about 7 years
    Hi there, looking at your example you cant have a list of task hashes without keys, the tasks would need to be the keys to what ever values you would want to allocate e.g db.systems.insert( {"name": "sub_system_3", "data" : {"high" :{"task1": "some value","task2": "some other value"}, "mid": {"task1": "some value","task2": "some other value"}, "low" : {"task1": "some value","task2": "some other value"}}})