"airplane": /747|380/ Vs "airplane": { $in: ["747", "380"] }


Can someone help me understand what is the difference in the syntax considering ‘air_routes’ collection among these two $match expressions - I’m seeing different result:

$match: {
“airplane”: /747|380/


$match: {
“airplane”: { $in: [“747”, “380”] }


I think I found one difference by looking at data:

pattern match finds airplane field with values like:
“airplane” : “380”
“airplane” : “380 744”

whereas, $in operator matches only:
“airplane” : “380”

However, someone writing an exam, how well can they familiarize data structure? Are there any tips or techniques?

Yes, you can find a complete explanation here

  • {$in:["A","B"]} is fine in arrays or single strings: ["D","A","X"], "A", "B"
  • but not on a string like this "C B A", B A etc.

One simple way to start off is by running findOne() or using the gui (Atlas or Compass).

Other 2 useful commands are:

  • find({field:{$exists:true}).count() compared with find({})
  • the other one is similar, but using $type
1 Like

Thanks for your pointers @Santiago_Miranda

My second question is, are there any techniques to quickly understand the data structure, especially while attempting such questions in a timed exam.

I have been using db.air_routes.count({airplane: {$exists: true}});

Just a quick suggestion from me here. I got stumped by this.

I realised later I could have used MongoDbCompass to analyse the schema to understand the contents of the that document property.

There is a tab in the application which will allow you to scan a collection quickly and get a rough idea of the kind of data you have in each field, with sample values.

Inspecting the sample values would have really helped me in this case.

1 Like

Thanks for your reply @Laurie_09536. I did use MongoDB Compass while I was going through basics, later instructions are all focused using terminal using mongo tool. So, I was trying to find if there is any cheat sheet to analyze data using mongo. Next time, I will try to use compass tool.

I have been doing that too, but unfortunately, Compass still does not tell you which formats it will accept; it only tells you when it does not accept one.