user7064075
user7064075

Reputation:

How to insert document or fields in nested documents in mongo

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

Upvotes: 6

Views: 20156

Answers (1)

djtsheps
djtsheps

Reputation: 117

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);

Upvotes: 9

Related Questions