Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB--add/drop elements in a field of array of hash how to
    text
    copied!<p>I am using perl MongoDBx::Class, following the tutorial I inserted the document below. The tags field is an array of hash. Tried to remove tag and add tag using mongodb's $pull and $addToSet function without success. </p> <p>How to add/drop elements to/from tags field? If you are not a perl programmer, answer in mongodb shell command also wlcome.</p> <p>Thanks.</p> <pre><code>my $novel = $novels_coll-&gt;insert({ _class =&gt; 'Novel', title =&gt; 'The Valley of Fear', year =&gt; 1914, author =&gt; { first_name =&gt; 'Arthur', middle_name =&gt; 'Conan', last_name =&gt; 'Doyle', }, added =&gt; DateTime-&gt;now(time_zone =&gt; 'Asia/Jerusalem'), tags =&gt; [ { category =&gt; 'mystery', subcategory =&gt; 'thriller' }, { category =&gt; 'mystery', subcategory =&gt; 'detective' }, { category =&gt; 'crime', subcategory =&gt; 'fiction' }, ], }); </code></pre> <p>This is the document inserted:</p> <pre><code>{ "_id": { "$oid": "4e27eae3008a6ee40f000000" }, "_class": "Novel", "added": "2011-07-21T12:01:23+03:00", "author": { "middle_name": "Conan", "last_name": "Doyle", "first_name": "Arthur" }, "tags": [ { "subcategory": "thriller", "category": "mystery" }, { "subcategory": "detective", "category": "mystery" }, { "subcategory": "fiction", "category": "crime" } ], "title": "The Valley of Fear", "year": 1914 } </code></pre> <p>Edit: </p> <p>After deeper exploration of MongoDBX, the update method overrided offical MongoDB driver, so the $pull, $addToSet may not work. I will use this stupid method:</p> <pre><code>my $novel = $novel_coll-&gt;find_one({ some criteria }); my @tags = $novel-&gt;tags; my @updated_tags = grep(unless($tag{category=&gt;CATEGORY}, @tags); #to pull out unwanted tag. Or use push to add new tag. $novel-&gt;update({tags=&gt;\@updated_tags}); </code></pre> <p>I hope MongoDBx has method to updated arrayRef field.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload