DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. MongoDB Tips & Tricks: Handling $type and Arrays

MongoDB Tips & Tricks: Handling $type and Arrays

Alec Noller user avatar by
Alec Noller
·
May. 23, 14 · Interview
Like (0)
Save
Tweet
Share
5.31K Views

Join the DZone community and get the full member experience.

Join For Free

According to Thomas Zahn, Co-Founder and CEO of 3T Software Labs, coming to MongoDB from a relational database background is a relief. In this recent article from the 3T Software Labs Blog, he points to the flexibility and dynamic nature of MongoDB as big selling points, as well as the JSON-based query language. However, that's not to say that MongoDB is completely free of cryptic design choices that may leave developers puzzled. 

In particular, Zahn's article focuses on MongoDB's $type query operator, which allows queries to filter by a certain types (only return Strings, for example). However, when it comes to Arrays, $type is a little strange: rather than returning fields that contain Arrays, it searches the individual elements of a field's Array for Arrays. In other words, an $type query for Arrays only finds two-dimensional Arrays - not exactly intuitive.

Zahn explores a number of solutions to this problem - reverting to JavaScript for a workaround, for example - and weighs the pros and cons (and performance drawbacks) of each. Also, Zahn takes a look at the reasons one might have for seeking out Arrays; they can have a major impact on certain operations in terms of performance, and Zahn explores those scenarios.

It's not that Zahn is out to smear the name of MongoDB - as is made clear at the start of his article, he's pretty fond of it - but some problems just seem so simple to fix:

Overall, I think it would be really great to have a dedicated $isArray query operator. The peculiar (and at times inconsistent) way arrays are handled by the search engine currently make it unnecessarily complex to guard one’s update queries against the tricky intricacies of arrays.

Check out Zahn's full article for details on how to avoid Array mishaps with $type in MongoDB.

MongoDB

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Beginner’s Guide To Styling CSS Forms
  • Configure Kubernetes Health Checks
  • Host Hack Attempt Detection Using ELK
  • Documentation 101: How to Properly Document Your Cloud Infrastructure Project

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: