2017-01-27

Meteor - MongoDB update using a computed/dynamic field name

v = "secondPart";
collectionName.update(
{ filterHere: "example" }
, {
$set: {
    [ 'firstPart.' + v + '.thirdPart' ]: "fieldValue"
}
}
);

2017-01-13

MongoDB - How to rename a field within an array of objects/sub-documents?

db.collection_name.find({

// Filter to only work on documents that contain the array field
"parent_field.child_old_field_name": { $exists : 1 }

}).forEach(
    function( doc ){

    // For each object in the current document's array
        for( i=0; i < parent_field.length; ++i ) {

            // Create the new field
            doc.parent_field[i].child_new_field_name = doc.parent_field[i].child_old_field_name;

            // Delete the old field
            delete doc.parent_field[i].child_old_field_name;
        }

        // Update the document
        db.collection_name.update(
        // Only the current document
        { _id : doc._id }
        // The updated document (as per the statements in the above "for" loop)
        , doc 
        );
    }
);



// This is how the collection that fits the example looks like:
{
    "_id" : "bs2XTNDYLSrp9Ae9m"
    , "parent_field" : [ 
        {
            "child_old_field_name" : "any value" 
        }
        , {
            "child_old_field_name" : "Any other value"
        }        
    ]
}

2017-01-10

MongoDB - How to make a case insensitive query/filter

db.collection_name.find({
    "field_name": /^search_term_here/i
});

PS. Make sure to use "^" in order for the query to use the index, if available, to avoid a full scan.

MongoDB - How to update a field using the value from another field

db.collection_name.find({filter_here_if_any}).forEach( function(doc) {
    doc.target_field_name = doc.source_field_name;
    db.collection_name.save( doc );
});

2017-01-05

CSS - Mixing fixed and percentage widths


<style>
  .comment {
    position: relative;
    padding-left: 100px;
  }
  .comment-left {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    border: 1px solid blue;
  }
  .comment-right {
    width: 100%;
    float: left;
    border: 1px solid red
  }
</style>


<div class="comment">

  <div class="comment-left">
    something
  </div>

  <div class="comment-right">
    something else
  </div>

</div>